Home > Article > Web Front-end > js implements adding a click event to the list and returning the corresponding content
This article mainly shares with you how to implement js to add a click event to the list and return the corresponding content. This is the interview question "Add a click event to the list so that it displays the corresponding content". I hope it can help everyone.
Method 1:
var lis = document.getElementsByTagName("li"); for(var i = 0; i<itemli.length; i++){ lis[i].index = i; //给每个li定义一个属性索引值,赋 lis[i].onclick = function(){ alert("下标索引值为:"+this.index+"\n"+"文本内容是:"+this.innerHTML); \n换行 索引值从0开始 } }
Related recommendations:
How to add a click event in js
## Detailed explanation of common errors in #js dynamically adding click events
Detailed explanation of how to add click events to images in html
The above is the detailed content of js implements adding a click event to the list and returning the corresponding content. For more information, please follow other related articles on the PHP Chinese website!