简体   繁体   中英

Javascript click event listener on multiple elements and get target 'rel' attribute

Code:

var articles = document.getElementsByTagName('li');
for (var i = 0; i < articles.length; i++) {
    articles[i].addEventListener('click',redirect,false);
}
function redirect(){
    alert(this.rel);
}

This code works for attribute id but not for attribute rel. Can anyone help me to get rel attribute?

您应该使用element.getAttribute('attribute_name')访问这些属性element.getAttribute('attribute_name')

alert(this.getAttribute('rel'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM