2021年6月8日18:34 记录一个 JS 的问题
下面的 JS 有问题
<ul>
<li><button class = "good">点击我</button></li>
<li><button class = "good">点击我</button></li>
<li><button class = "good">点击我</button></li>
<li><button class = "good">点击我</button></li>
</ul>
<script>
let btns = document.getElementsByClassName("good");
let btnsLen = btns.length;
let jagon = [
'666',
'777',
'888',
'999'
];
for (let i = 0; i < btnsLen; i++) {
(function () {
let j = i;
btns[j].addEventListener("click", manOpen(jagon[j]))
})()
}
function (jagon) {
alert(jagon);
}
<script>
下面是正常运作的 JS
<ul>
<li><button class = "good">点击我</button></li>
<li><button class = "good">点击我</button></li>
<li><button class = "good">点击我</button></li>
<li><button class = "good">点击我</button></li>
</ul>
<script>
let btns = document.getElementsByClassName("good");
let btnsLen = btns.length;
let jagon = [
'666',
'777',
'888',
'999'
];
for (let i = 0; i < btnsLen; i++) {
(function () {
let j = i;
btns[j].addEventListener("click", function () {
alert(jagon[j]);
})
})()
}
<script>
解决方法: 我也不知道哪里为什么没有生效