例如,我可以在一个文档中写入多个 ID
(document.getElementById("myDIV1" , "myDIV2").style.display = "none";
最佳答案
没有。 getElementById()
只需要一个 id。
您可以使用 querySelectorAll()
代替,您只需要循环遍历结果:
var divs = document.querySelectorAll('#myDIV1, #myDIV2')
[].forEach.call(divs, function(div) {
div.style.display = 'none';
});
同样,当您标记 jQuery 时,您可以使用它:
$('#myDIV1, #myDIV2').hide();
关于javascript - 我可以在一个文档中写几个 ID,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47469208/