Home > Web Front-end > JS Tutorial > body text

How to bind click events in js (detailed tutorial)

亚连
Release: 2018-06-12 11:56:35
Original
3393 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

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

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!

Related labels:
js
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!