我想知道究竟是什么之间的差异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")有效.有什么想法吗?
我试图遍历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)