If you need to assign only one click
event, you can assign onclick
:
If you have an ID:
myAnchor = document.getElementById("Anchor");
myAnchor.onclick = function() { myFunc(); return false; }
you can also walk through all anchors:
anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].onclick = .....
}
There's also a document.getElementsByClassName
to simulate jQuery's class selector but it is not supported by all browsers.
If it could be that you need to assign multiple events on one element, go with addEventListener
shown by @Jordan and @David Dorward.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…