Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How jQuery simulates trigger events

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要介绍了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如何模拟触发事件"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report