简体   繁体   English

jQuery 以编程方式触发事件

[英]jQuery programmatically trigger events

What all events can be triggered programmatically using jQuery?什么所有事件都可以使用 jQuery 以编程方式触发? Also is there any important differences to be remembered when one is doing event triggering using jQuery Vs a natural way of it being triggered?当一个人使用 jQuery Vs 进行事件触发时,是否有任何重要的区别需要记住?

Every event can be programmatically fired, just use the callback-less version of it.每个事件都可以以编程方式触发,只需使用它的无回调版本即可。

Example:例子:

$('#button').click(function() { alert('event hanlder'); });

$('#button').click(); // generate the event

About your second question, there should be no difference between the native and jQuery event handlers.关于您的第二个问题,本机和 jQuery 事件处理程序之间应该没有区别。


One thing that is neat though is that jQuery binds this to the element that received the event, inside the callback (this doesn't happen in native event handlers):一件很巧妙的事情是 jQuery 将this绑定到接收事件的元素,在回调内部(这不会发生在本机事件处理程序中):

$('#button').click(function() { alert(this); }); // here 'this' == document.getElementById('button');

Warning: the element referenced by this is not "jQuery augmented".警告:引用的元素this是不是“jQuery的增强”。 If you want to traverse or modify it with jQuery goodness you'll have to do something like var $this = $(this);如果你想用 jQuery 的优点遍历或修改它,你必须做类似var $this = $(this);事情var $this = $(this);

You should know the differences between trigger and triggerHandler in jQuery.您应该知道 jQuery 中triggertriggerHandler之间的区别。

trigger扳机

trigger attempts to replicate the natural event as best as it can. trigger尽可能最好地复制自然事件的尝试。 The event handler for the event being triggered get's executed, but the default browser actions will not always be replicated exactly.被触发的事件的事件处理程序会被执行,但默认的浏览器操作不会总是被完全复制。 For example $('a#link).trigger('click');例如$('a#link).trigger('click'); will execute the javascript function bound to the links click event handler, but will not redirect the browser to the href of the anchor, like a normal click would.将执行绑定到链接click事件处理程序的 javascript 函数,但不会像普通单击那样将浏览器重定向到锚点的href EX: http://jsfiddle.net/AxFkD/例如: http : //jsfiddle.net/AxFkD/

All the short forms of the trigger call behave exactly like trigger IE. trigger调用的所有简短形式的行为都与trigger IE 完全相同。 click() , mouseup() , keydown() , etc click() , mouseup() , keydown()

triggerHandler触发器处理程序

triggerHandler prevents bubbling up ( EX. http://jsfiddle.net/LmqsS/ ), it avoids default browser behaviour and just executes the events callback, and it returns the return value of the event handler instead of a jQUery object for chaining. triggerHandler防止冒泡(EX. http://jsfiddle.net/LmqsS/ ),它避免了默认的浏览器行为,只执行事件回调,它返回事件处理程序的返回值而不是用于链接的 jQUery 对象。

You should also be aware that trigger affects all elements matched by a selector, but triggerHandler only affects the first one EX: http://jsfiddle.net/jvnyS/您还应该知道trigger会影响选择器匹配的所有元素,但triggerHandler只影响第一个 EX: http : //jsfiddle.net/jvnyS/

You can trigger any event programmatically.您可以以编程方式触发任何事件。 But most of the events cannot be simulated as the natural event using programmatic triggers.但是大多数事件不能使用程序触发器模拟为自然事件。

//to trigger a click event on a button //触发一个按钮的点击事件

$("buttonSelector").trigger("click");

First, for obvious reasons, you cannot trigger the ready event.首先,由于显而易见的原因,您无法触发就绪事件。

That said, events raised by trigger() behave the same way as if they were triggered by the user.也就是说, trigger() 引发的事件的行为方式与它们由用户触发的方式相同。 In particular, the event handlers are called in the same order.特别是,事件处理程序以相同的顺序调用。

The only difference I know of is that triggered events did not bubble up the DOM tree in older versions of jQuery (that behavior was fixed in version 1.3).我所知道的唯一区别是,在旧版本的 jQuery 中,触发事件不会使 DOM 树冒泡(该行为在 1.3 版中已修复)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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