简体   繁体   中英

Call an already defined .onClick() function from jQuery

I created some ajax paginated comments in WordPress. Unfortunately, if the user had clicked on the reply button and then goes about to click on another comment page, the Comment Form vanishes into thin air.

Anyways, simple question: Triggering the "Cancel Reply" function from my code each time the user clicks on a new ajax page would effectively solve the problem by causing the form to jump back to the original position.

How can I trigger cancel.onclick() from my own code easily? I was going to just duplicate commands and create a new function, but thought there might be an easier way to save a few bytes!

Here's the source code: http://core.trac.wordpress.org/browser/trunk/wp-includes/js/comment-reply.dev.js

Try something like this:

$('#id_of_your_cancel_button').click();
// same thing as $('#id_of_your_cancel_button').trigger('click');

If the "cancel" logic is to be used in multiple contexts, perhaps it would be best if it lives in its own named function declaration, rather than the anonymous function expression as in your example. This would give you the option of doing something like this:

function myCancelCode(){
  do_stuff();
}

Then in your addComment object:

cancel.onclick = myCancelCode;

and from anywhere else:

if( somethingHappens ){
  myCancelCode();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM