gpt4 book ai didi

javascript - 如何通过 for 循环、mr[2] 和 test1[2] 添加 onclick

转载 作者:行者123 更新时间:2023-12-03 03:22:34 25 4
gpt4 key购买 nike

如何添加onclick通过for循环,for mr[2]test1[2]

我有更多按钮和 iframe

mr[2].onclick = function selectItem() {
for (var i = 0; i < mr.length; i++) {
mr[i].classList.remove('selected');
mr[i].style.backgroundColor = 'rgba(21,21,21,1)';
};

this.classList.add('selected');
this.style.backgroundColor = '#ff5722';

for (var i = 0; i < test1.length; i++) {
test1[i].setAttribute('src', 'https://upload.wikimedia.org/wikipedia/commons/0/0c/Olive_green_check.svg');
test1[i].style.display = 'none';
};

test1[2].setAttribute('src', 'www.google.com');
test1[2].style.display = 'block';
};

最佳答案

考虑简单地在要添加“onclick”属性的对象上使用 DOM Element 方法 addEventListener 作为可能的解决方案:

https://www.w3schools.com/JSREF/met_document_addeventlistener.asp

关于javascript - 如何通过 for 循环、mr[2] 和 test1[2] 添加 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46503745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com