用于循环和传递值的addEventListener
// Function to run on click:function makeItHappen(elem, elem2) { var el = document.getElementById(elem); el.style.backgroundColor = "red"; var el2 = document.getElementById(elem2); el2.style.backgroundColor = "blue";}// Autoloading function to add the listeners:var elem = document.getElementsByClassName("triggerClass");for (var i = 0; i < elem.length; i += 2) { var k = i + 1; var boxa = elem[i].parentNode.id; var boxb = elem[k].parentNode.id; elem[i].addEventListener("click", function() { makeItHappen(boxa, boxb); }, false); elem[k].addEventListener("click", function() { makeItHappen(boxb, boxa); }, false);}
<div class="container"> <div class="one" id="box1"> <p class="triggerClass">some text</p> </div> <div class="two" id="box2"> <p class="triggerClass">some text</p> </div></div><div class="container"> <div class="one" id="box3"> <p class="triggerClass">some text</p> </div> <div class="two" id="box4"> <p class="triggerClass">some text</p> </div></div>
哈士奇WWW
相关分类