本文介绍了在getElementById中添加多个id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将此脚本与多个ID一起使用?对我尝试添加的第二个ID不起作用
How to use this script with multiple ids? Does not work on the second id that I try to add
<script type="text/javascript">
window.onload = (function () {
var elem = document.getElementById("id1","id2").onclick = function()
{
fbq('track', 'InitiateCheckout');
}
});
</script>
推荐答案
您可以使用 querySelectorAll
按ID分类选择多个项目:
You can use querySelectorAll
to select many items by their IDs:
var items = document.querySelectorAll('#id2, #id3, #id5');
for (var i = 0; i < items.length; i++)
{
items[i].onclick = function() {
this.innerText = this.innerText + '!';
};
}
2, 3, 5 are working:
<p id="id1">I am 1</p><p id="id2">I am 2</p><p id="id3">I am 3</p><p id="id4">I am 4</p><p id="id5">I am 5</p>
但是,创建公共类听起来要好得多。
However, creating a common class sounds much better.
这篇关于在getElementById中添加多个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!