Clever Use of Jquery to change the onclick event of an element

Source: Internet
Author: User

You need to click an image to publish the set. The page code is as follows:

 

Html code
  1. <S: property value = "IMAGES_PATH"/> <s: if test = '% {releaseStatus = "YES"}'> pubed.png </s: if>
  2. <S: else> nopub.png </s: else>"
  3. Onclick = "<s: if test = '% {releaseStatus =" YES "}'> changeToClose (<s: property value =" suiteId "/>, $ (this); </s: if>
  4. <S: else> changeToOpen (<s: property value = "suiteId"/>,$ (this); </s: else> "/>

 

After the set group is released, you need to change the method for calling the next click event.

 

Use: img. click (function () {changeToOpen (suitId, img );});

 

Result: The method defined by the onclick event defined by img is executed first, then the method bound by the previous onclick is executed, and finally the binding method is executed.

 

Find the unbind code: unbind ('click ')

Img. unbind ('click'). click (function () {changeToOpen (suitId, img );});

 

Result: The onclick event definition method defined by img is executed first, and then the binding method is executed.

 

Find the code for unbinding onclick:

 

Img. attr ('onclick', ''). unbind ('click'). click (function () {changeToOpen (suitId, img );});

 

 

Js Code
  1. Function changeToOpen (suitId, img ){
  2. // Use train_com ac_type to transmit 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. // PilotInfForVoteDto is used to transmit the 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.