Home > Web Front-end > JS Tutorial > About js index subscript li collection binding click event example sharing

About js index subscript li collection binding click event example sharing

小云云
Release: 2018-01-15 09:20:15
Original
1634 people have browsed it

This article uses a piece of example code to explain to you the relevant knowledge of binding click events to the li collection of js index subscripts. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it. I hope it can help. to everyone.

The following code introduces you to the li collection binding click event. The specific code is as follows:


//Method-1:
var items = document.getElementsByTagName('li');
  for(var i=0;i<items.length;i++){
    items[i].index = i;
    items[i].onclick = function(){
      this.innerHTML = this.index;
  }
}
//Method-2:
var items = document.getElementsByTagName(&#39;li&#39;);
  for(var i = 0; i<items.length; i++){
    (function(index){
      items[i].onclick = function(){
      this.innerHTML = index;
    }
  })(i)
}
//Method-3:
var items = document.getElementsByTagName(&#39;li&#39;);
for(var i = 0; i<items.length; i++){
  items[i].onclick = function(index){
    return function(){
      this.innerHTML = index;
    }
  }(i)
}
Copy after login

Related recommendations:

jQuery simple binding single event example sharing

Example explanation jQuery realizes html two-way binding function

Examples to explain the usage of JavaScript function binding

The above is the detailed content of About js index subscript li collection binding click event example sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template