Clever use of jquery to change an element's OnClick event

Source: Internet
Author: User

Need to click on the image to publish the set of groups, page code:

HTML code
  1. < img width="20px" src= "
  2. < S:property Value="Images_path" /><s:if Test='%{ Releasestatus = = "YES"} '>pubed.png</s:if>
  3. < S:else >nopub.png</s:else>"
  4. onclick= "<s:if test= '%{releasestatus = =" YES "} '>changetoclose (< s:property value= "suiteid" />, $ (this)); </ s:if >
  5. < S:else >changetoopen (<s:property value="Suiteid" / >, $ (this)); </ S:else >" />

After the set is released, you need to change the method of the next click event Invocation.

Used: Img.click (function () {Changetoopen (Suitid, IMG);});

Result: Executes the method defined by the OnClick event defined by IMG, then executes the method previously bound by the onclick, and finally executes the method of this binding.

Find unbound code: Unbind (' click ')

Img.unbind (' click '). Click (function () {Changetoopen (Suitid, IMG);});

Result: The method defined by the OnClick event defined by IMG is executed first, and then the method of this binding is executed.

Find the code to dismiss the onclick binding:

Img.attr (' onclick ', '). Unbind (' click '). Click (function () {Changetoopen (Suitid, IMG);});

JS Code
  1. function changetoopen (Suitid, img) {
  2. //Use train_com Ac_type to implement the pass lock information
  3. params = "train_com="+suitid+ "&ac_type=yes"; //alert (params);
  4. $.post ("dosimulatorpubbyid.do", params,function(data) {
  5. Eval ("var rst ="+data);
  6. alert (rst.msg);
  7. if (Rst.status = = 3) {
  8. Img.attr ("src","<s:property value="images_path"/>pubed.png");
  9. Img.attr ('onclick ', '). Unbind (' click '). Click ( function () {Changetoclose ( Suitid, IMG); });
  10. }
  11. });
  12. }
  13. function changetoclose (Suitid, img) {
  14. //Here with the help of pilotinfforvotedto to achieve the transmission lock information
  15. params = "train_com="+suitid+ "&ac_type=no"; //alert (params);
  16. $.post ("dosimulatorpubbyid.do", params,function(data) {
  17. Eval ("var rst ="+data);
  18. alert (rst.msg);
  19. if (Rst.status = = 3) {
  20. Img.attr ("src","<s:property value="images_path"/>nopub.png");
  21. Img.attr ('onclick ', '). Unbind (' click '). Click ( function () {Changetoopen ( Suitid, IMG); });
  22. }
  23. });
  24. }

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.