javascript - How to handle all links in a class? -
i know how handle link id:
var = document.getelementbyid("mylink"); a.onclick = function() { alert("ok"); };
but if have 3 links within 1 class .lotsoflinks , want handle each 1 did id.
what's best , short way?
thank you.
// use queryselectorall greater browser compatibility var = document.queryselectorall(".mylink"); // make handler var handler = function() { alert("ok"); }; // iterate collection, , assign handler (var = 0; < a.length; i++) { a[i].onclick = handler; }
note queryselectorall
doesn't work in ie6/7, imagine you're not supporting javascript environments @ point.
Comments
Post a Comment