![](/img/trans.png)
[英]Javascript click event listener on multiple elements and get target ID
[英]Javascript click event listener on multiple elements and get target 'rel' attribute
碼:
var articles = document.getElementsByTagName('li');
for (var i = 0; i < articles.length; i++) {
articles[i].addEventListener('click',redirect,false);
}
function redirect(){
alert(this.rel);
}
此代碼適用於屬性ID,但不適用於屬性rel。 誰能幫我獲得rel屬性?
您應該使用element.getAttribute('attribute_name')
訪問這些屬性element.getAttribute('attribute_name')
alert(this.getAttribute('rel'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.