简体   繁体   中英

How to trigger a js function with Jquery

I want to trigger this jquery function by using trigger method of JQuery. How can I do that? Actually I dont even know the trigger method is suitable for user defined functions.

$('a.pop').click(function() {alert('testing'); }

this is not working

 $('a').trigger(testtt);

 var testtt = function(){ alert('aaa');} 

Very similar to the way you install the event handler:

$('a.pop').click();

If you have the name of the event you want to trigger as a string, you can also do it this way:

$('a.pop').trigger('click');

This is also the solution to use if you want to pass crafted data to the event handler -- trigger also accepts a second parameter.

You can trigger a click event on the element by simply running

$('a.pop').click()

$('a.pop').click() , or if you're triggering some dynamic method, or custom event:

$('a.pop').trigger(eventName) , eg: $('a.pop').trigger('click');

Reading from jQuery API, the following should work.

$('a.pop').trigger('click');

.trigger() is used to trigger event handlers (custom or built-in'). Since you bound your function to the "click" handler, you can use trigger like so to call it:

$('a.pop').trigger('click');

jQuery's event binding methods can also be called without parameters to trigger them, which means you can also do this:

$('a.pop').click();

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