This article describes how to add events to the tag Based on javascript. The function of adding events based on the closure loop involves javascript closures and event operation related techniques, for more information about how to add events to a tag in js, see the example in this article. We will share this with you for your reference. The details are as follows:
For example:
Effect: cyclically add an event to the tag with the ml-praise style class, and increase the number by 1 after clicking the tag.
The Html structure is as follows:
- Quantity: 100
- Quantity: 200
- Quantity: 300
- Quantity: 400
JS implementation code:
Function addPraiseNum () {var praiseObjs = document. getElementsByClassName ('ml-praise '); for (var I = 0; I <praiseObjs. length; I ++) {var praiseObj = praiseObjs [I]; praiseObj. onclick = (function (dingObj) {return function () {dingObj. innerHTML = parseInt (dingObj. innerHTML) + 1 ;};} (praiseObj. getElementsByClassName ('ding-num') [0]);}
The implementation result is as follows:
I hope this article will help you design JavaScript programs.
For more articles about how to add events to a tag in js (using closure loops), refer to PHP!