相关疑难解决方法(0)

JavaScript中的querySelector和querySelectorAll vs getElementsByClassName和getElementById

我想知道究竟是什么之间的差异querySelector和querySelectorAll对抗 getElementsByClassName和getElementById?

从这个链接我可以收集,querySelector我可以编写document.querySelector(".myclass")获取类的元素,myclass并document.querySelector("#myid")获取ID元素myid.但我已经可以做到这一点getElementsByClassName和getElementById.应该首选哪一个?

我也在XPage中工作,其中ID是用冒号动态生成的,看起来像这样view:_id1:inputText1.所以当我写document.querySelector("#view:_id1:inputText1")它不起作用.但写作document.getElementById("view:_id1:inputText1")有效.有什么想法吗?

javascript

148
推荐指数
8
解决办法
10万
查看次数

JavaScript:循环遍历从getElementsByTagName返回的所有元素

我试图遍历getElementsByTagName("input")使用forEach 重新编译的所有元素.任何想法为什么这在FF,Chrome或IE中不起作用?

<html>
    <head>
    </head>
    <body>
        <input type="text" value="" />
        <input type="text" value="" />
        <script>
            function ShowResults(value, index, ar) {
                alert(index);
            }
            var input = document.getElementsByTagName("input");
            alert(input.length);
            input.forEach(ShowResults);
    </script>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

javascript arrays foreach getelementsbytagname

52
推荐指数
8
解决办法
6万
查看次数