innerHTML不能与JS中的classname一起使用

编程入门 行业动态 更新时间:2024-10-10 10:24:37
本文介绍了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一起使用

本文发布于:2023-10-30 18:50:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543698.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:能与   innerHTML   classname   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!