📌  相关文章
📜  在javascript代码示例中按类名单击侦听器

📅  最后修改于: 2022-03-11 15:02:50.147000             🧑  作者: Mango

代码示例1
var elements = document.getElementsByClassName("classname");

var myFunction = function() {
    var attribute = this.getAttribute("data-myattribute");
    alert(attribute);
};

for (var i = 0; i < elements.length; i++) {
    elements[i].addEventListener('click', myFunction, false);
}