Home  >  Article  >  Web Front-end  >  js implements adding a click event to the list and returning the corresponding content

js implements adding a click event to the list and returning the corresponding content

小云云
小云云Original
2018-03-17 16:30:213214browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn