如何在jQuery触发器上运行回调函数(“click”)? [英] How to run a callback function on a jQuery trigger("click")?

查看:1198
本文介绍了如何在jQuery触发器上运行回调函数(“click”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在触发调用的回调中触发自定义事件,但我无法让它工作。



我试过这样:

  var $ input = $(.ui-popup-container)。 find(input).eq(2); 

function runtests(){
console.log(clicked the input);
};
$ input.trigger('click',runtests());

并且:

  var $ input = $(.ui-popup-container).find(input).eq(2); 
$ input.trigger('click',function(){
console.log(clicked the input);
}
pre>

它们都不工作。



问题: $ b当我触发对某个元素的点击时,如何获得一个回调函数?

解决方案

trigger ,绑定事件处理程序会立即执行,因此您不需要任何回调。只需使用

  $ input.trigger('click'); 
runtests();


I need to trigger a custom event in the callback of a trigger call, but I can't get it to work.

I tried this:

var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);

function runtests () {
    console.log("clicked the input");
};
$input.trigger('click', runtests()); 

and this:

var $input = $( ".ui-popup-container" ).find( "input" ).eq(2);
$input.trigger('click', function(){
    console.log("clicked the input");
}

Neither of which works.

Question:
How do I get a callback function to run when I'm triggering a click on an element?

解决方案

When you call trigger, the bound event handler is immediately executed, so you don't need any callback. Just use

$input.trigger('click');
runtests();

这篇关于如何在jQuery触发器上运行回调函数(“click”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆