getelementbyclassname而不是getelementbyid无法正常工作 [英] getelementbyclassname instead of getelementbyid is not working

查看:94
本文介绍了getelementbyclassname而不是getelementbyid无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多次,现在您可以获取getElementsByClassName.如果我用Id替换ClassName,但是使用单词ClassName不能正常工作,那么下面的方法可以正常工作.有人知道为什么吗? (我在Chrome和Firefox上尝试过)

I have read many times that you can NOW get getElementsByClassName. This below works fine IF I replace ClassName by Id, but using the word ClassName does not work. Anyone know why? (I tried on Chrome and Firefox)

      <script type="text/javascript">
        function makeDisable(){
        var x=document.getElementsByClassName("mySelect");
        x.disabled=true
        }
        function makeEnable(){
            var x=document.getElementsByClassName("mySelect");
             x.disabled=false
        }
     </script>

     <form>
        <select class="mySelect" id="mySelect">
        <option>Apple</option>
        <option>Banana</option>
        <option>Orange</option>
    </select>

        <input type="button" onclick="makeDisable()" value="Disable list">
        <input type="button" onclick="makeEnable()" value="Enable list">
    </form>

推荐答案

该函数称为getElementsByClassName.复数.它不返回元素,而是返回所有具有类名称的元素的数组.

The function is called getElementsByClassName. Plural. It returns not an element, but an array of all the elements that have the class name.

因此,即使数组仅包含一项,即使数组中只有一个元素,您仍然需要对其进行索引.

So even if the array consists of only one item, even there is only one element in the array, you still need to index it.

 x[0].disabled=true

代替

 x.disabled=true

小提琴

这篇关于getelementbyclassname而不是getelementbyid无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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