javascript - 我不能添加超过 2 个 document.querySelector().addEventListener()
问题描述
您好,我正在使用 sweetalert ver2,我想在一个页面中放置几个按钮,当每个按钮单击时运行引用它的特定 js sweetalert 代码。
我这样做的方式是制作一个带有 ID 的按钮:
<button id="test1">TEST 1 </button>
<button id="test2">TEST 2 </button>
and it goes on to several buttons...
然后我使用这样的querySelector:
document.querySelector('#test1').addEventListener('click',runTestOne);
document.querySelector('#test2').addEventListener('click',runTestTwo);
and it goes for other buttons...
sweetalert js 代码是
function runTestOne () {
swal({
html:
'<p>TEST1 CONTENT</p>'
});
}
function runTestTwo () {
swal({
html:
'<p>TEST2 CONTENT</p>'
});
}
and other sweetalert functions for other buttons...
所以我想添加更多这些代码并使用多个按钮用于不同的目的,但如果我添加超过 2 个具有相同代码模式的按钮,它就不起作用。出了什么问题?
我使用 window.onload 并将 2 个 querySelector 放入其中,另一个在它之外工作,但在我添加另一个按钮它不会工作。
解决方案
I solved it with separating each java-script sweet-alert code in <script></script>
s