开发者

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/tr开发者_StackOverflow中文版unk/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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜