Get the App
SLTechnology News&Howtos  ›  Development  › 

How jQuery simulates trigger events

Shulou Source: shulou.com Published: 2022-06-01 04:17:40 09月22日 Update

这篇文章主要介绍了jQuery如何模拟触发事件,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

模拟触发事件

我们可以通过trigger模拟触发一个click事件

var press = $('#press');press.on('click',function(e, how){ how = how || ''; alert('The buton was clicked ' + how + '!');});press.trigger('click');press.trigger('click',['fast']);

同时,我们亦可以,使用on()方法创建自己喜欢的事件名称,然后通过trigger来触发。举例如下:

Jump Punch Click Clear var button1 = $('#button1'), button2 = $('#button2'), button3 = $('#button3'), clear = $('#clear'), div = $('#eventDiv');div.on({ jump : function(){ alert('Jumped!'); }, punch : function(e,data){ alert('Punched '+data+'!'); }, click : function(){ alert('Simulated click!'); }});button1.click(function(){ div.trigger('jump');});button2.click(function(){ // Pass data along with the event div.trigger('punch',['hard']);});button3.click(function(){ div.trigger('click');});clear.click(function(){ //some clear code});感谢你能够认真阅读完这篇文章,希望小编分享的"jQuery如何模拟触发事件"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

Tags: Events articles at the same time value interest available through name method more friends knowledge editing industry information information channels channels references learning help support Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple vpn Shulou Technology NVidia