Home >Web Front-end >JS Tutorial >How to bind click events in js (detailed tutorial)

How to bind click events in js (detailed tutorial)

亚连
亚连Original
2018-06-12 11:56:353481browse

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

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)
}

The above is what I compiled for you. I hope it will be helpful to you in the future.

Related articles:

What are the application scenarios of child processes in Node.js

Detailed interpretation of the file system and related issues in nodeJs Stream

What are the Javascript debugging commands?

How to use http module in node.js

How to use js to invoke App in WeChat?

How to use cli to reconstruct multi-page scaffolding in vue

How to change the size of objects by dragging in JS

The above is the detailed content of How to bind click events in js (detailed tutorial). 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