Home > Article > Web Front-end > How to bind click events in js (detailed tutorial)
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('li'); for(var i = 0; i<items.length; i++){ (function(index){ items[i].onclick = function(){ this.innerHTML = index; } })(i) } //Method-3: var items = document.getElementsByTagName('li'); 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!