Js implements the method of adding events to the tag (using the closure loop)

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.