This article describes the method of adding events to the a tag by js. Share it for your reference, as follows:
Explain with examples:
Implement the effect: Loop adds events to the a tag with the ml-praise style class, and after clicking the a tag, the corresponding number increases by 1.
The structure of the Html is as follows:
<ul> <li><a href="javascript:;">Quantity: <span>100</span></a></li> <li><a href="javascript:;">Quantity: <span>200</span></a></li> <li><a href="javascript:;">Quantity: <span>300</span></a></li> <li><a href="javascript:;">Quantity: <span>400</span></a></li></ul>
JS implementation code:
function addPraiseNum() { var prayObjs = document.getElementsByClassName('ml-praise'); for (var i = 0; i < prayObjs.length; i++) { var prayObj = prayObjs[i]; prayObj.onclick = (function (dingObj) { return function () { dingObj.innerHTML = parseInt(dingObj.innerHTML) + 1; }; })(praiseObj.getElementsByClassName('ding-num')[0]); }}The effect is as follows:
For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript Mathematical Operation Usage", "Summary of JavaScript Data Structure and Algorithm Skills", "Summary of JavaScript Switching Special Effects and Skills", "Summary of JavaScript Search Algorithm Skills", "Summary of JavaScript Animation Special Effects and Skills", "Summary of JavaScript Errors and Debugging Skills" and "Summary of JavaScript Traversal Algorithm and Skills"
I hope this article will be helpful to everyone's JavaScript programming.