innerHTML不能与JS中的classname一起使用 [英] innerHTML not working with classname in JS

查看:72
本文介绍了innerHTML不能与JS中的classname一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的下拉列表选择特定值 -

My drop down List to select particular value-

<select name="category" id="category" onChange="showDiv(this.value);" >
    <option value="">Select This</option>
    <option value="1">Nokia</option>
    <option value="2">Samsung</option>
    <option value="3">BlackBerry</option>
    </select>

这是我要显示文字的div

This is the div where i want to show the text


< span class =catlink> < / span>

这是我的JS功能 -

And this is my JS function -

    function showDiv( discselect )
    {

    if( discselect === 1)
    {
    alert(discselect); // This is alerting fine
    document.getElementsByClassName("catlink").innerHTML = "aaaaaaqwerty"; // Not working
    }

}

告诉我为什么这不起作用,我做错了什么?

Let me know why this is not working, and what i am doing wrong?

推荐答案

document.getElementsByClassName(catlink )在网页中选择所有元素数组,因此您必须使用 [0]

document.getElementsByClassName("catlink")is selecting all the elements in webpage as array therefore you have to use [0]

 function showDiv( discselect ) 
 { 

 if( discselect === 1) 
 { 
 alert(discselect); // This is alerting fine 
 document.getElementsByClassName("catlink")[0].innerHTML = "aaaaaaqwerty"; // Now working 
 } 
 }

这篇关于innerHTML不能与JS中的classname一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆