本文介绍了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一起使用
发布评论