[英]How to trigger a js function with Jquery
I want to trigger this jquery function by using trigger method of JQuery.我想通过使用JQuery的触发方法来触发这个jquery function。 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.如果您想将精心制作的数据传递给事件处理程序,这也是使用的解决方案——
trigger
也接受第二个参数。
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').click()
,或者如果您正在触发一些动态方法或自定义事件:
$('a.pop').trigger(eventName)
, eg: $('a.pop').trigger('click');
$('a.pop').trigger(eventName)
,例如: $('a.pop').trigger('click');
Reading from jQuery API, the following should work.从 jQuery API 读取,以下应该工作。
$('a.pop').trigger('click');
.trigger()
is used to trigger event handlers (custom or built-in'). .trigger()
用于触发事件处理程序(自定义或内置)。 Since you bound your function to the "click" handler, you can use trigger
like so to call it:由于您将 function 绑定到“点击”处理程序,因此您可以使用
trigger
来调用它:
$('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: jQuery的事件绑定方法也可以不带参数调用来触发它们,也就是说你也可以这样做:
$('a.pop').click();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.