我有8个活动的按钮,我想改变img src对当前活动的按钮,并扭转src时,它不活跃了。我不擅长js,我想不出来。
所有非活动图像的结尾都是“\u grey.png”,活动图像的结尾应该是“\u blue.png”。
html格式:
<div id="worldfilter">
<button class="wf-btn active"><img src="world_blue.png"> Welt </button>
<button class="wf-btn"><img src="europe_grey.png"></button>
<button class="wf-btn"><img src="north_america_grey.png"></button>
<button class="wf-btn"><img src="south_america_grey.png"></button>
<button class="wf-btn"><img src="africa_grey.png"></button>
<button class="wf-btn"><img src="orient_grey.png"></button>
<button class="wf-btn"><img src="asia_grey.png"></button>
<button class="wf-btn"><img src="oceania_grey.png"></button>
</div>
js公司:
//Worldfilter button
// Get the container element
var btnContainer = document.getElementById("worldfilter");
// Get all buttons with class="wf-btn" inside the container
var btns = btnContainer.getElementsByClassName("wf-btn");
// Loop through the buttons and add the active class to the current/clicked button
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
提前谢谢!!
2条答案
按热度按时间h4cxqtbf1#
3pmvbmvn2#
你可以这样做(我注解了代码)。