手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jquery中trigger()无法触发hover事件的解决方法
jquery中trigger()无法触发hover事件的解决方法
摘要:今天做一个项目,遇到了一个问题,是以前没有遇到过的,就此记上一笔。1、trigger方法解释官方是这么解释的:复制代码代码如下:Descri...

今天做一个项目,遇到了一个问题,是以前没有遇到过的,就此记上一笔。

1、trigger方法解释

官方是这么解释的:

复制代码 代码如下:

Description: Execute all handlers and behaviors attached to the matched elements for the given event type.

用法:

.trigger( eventType [, extraParameters] )

其中eventType包含javascript内置的事件、jQuery增加的事件和自定义事件。例如:

$('#foo').bind('click', function() { alert($(this).text()); }); $('#foo').trigger('click'); $('#foo').bind('custom', function(event, param1, param2) { alert(param1 + "n" + param2); }); $('#foo').trigger('custom', ['Custom', 'Event']);

很强大,常常用于页面初始化的时候使用。

2、trigger遇到hover

var $search=$('#header .search'); $search.find('li').hover(function() { alert(1); },function() { alert(2); }); $search.find('li').eq(0).trigger('hover');

无法触发hover。但是:

var $search=$('#header .search'); $search.find('li').click(function() { alert(1); },function() { alert(2); }); $search.find('li').eq(0).trigger('click');

触发click正常!

解决办法:

var $search=$('#header .search'); $search.find('li').hover(function() { alert(1); },function() { alert(2); }); $search.find('li').eq(0).trigger('mouseenter');//hover修改为mouseenter/mouseleave/mouseover/mouseout

同样的情况存在于jQuery.live(),不过live不推荐在1.7以后版本使用,使用on()代替。

以上所述就是本文的全部内容了,希望大家能够喜欢。

【jquery中trigger()无法触发hover事件的解决方法】相关文章:

jQuery fancybox在ie浏览器下无法显示关闭按钮的解决办法

JavaScript实现点击文字切换登录窗口的方法

javascript为按钮注册回车事件(设置默认按钮)的方法

jquery插件splitScren实现页面分屏切换模板特效

png在IE6 下无法透明的解决方法汇总

javascript验证邮件地址和MX记录的方法

jQuery实现延迟跳转的方法

jquery实现点击label的同时触发文本框点击事件的方法

jQuery实现不断闪烁文字的方法

js实现键盘Enter键提交表单的方法

精品推荐
分类导航