悬停时的Jquery不起作用
Jquery on hover not functioning
我正在更改我的代码以与jQuery 1.8兼容,但我被这个hover
卡住了,它不起作用。当我在click
上使用同样的东西时,它起了作用。这是我的密码,有人能告诉我哪里出了问题吗?
$(document).on('hover', '.top-level', function (event) {
$(this).find('.actionfcnt').show();
$(this).find('.dropfcnt').show();
}, function () {
$(this).find('.dropfcnt').hide('blind', function () {
$('.actionfcnt').hide();
});
});
自jQuery 1.8起已弃用 :名称"hover"用作字符串"mouseenter-moseleave"的缩写。它为这两个事件附加了一个单独的事件处理程序,处理程序必须检查event.type以确定事件是mouseenter还是mouseleave不要将"hover"伪事件名称与.hooper()方法混淆,后者接受一个或两个函数
来源:http://api.jquery.com/on/#additional-票据
这几乎说明了一切,你不能用"悬停":
$(document).on('mouseenter','.top-level', function (event) {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
}).on('mouseleave','.top-level', function(){
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
没有"悬停"事件。有一个.haverage()函数,它接受2个回调(如您的示例中所示)。
尝试:
$(".top-level").on({
mouseenter: function (event) {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
},
mouseleave: function (event) {
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
}
});
或
$(".top_level").on("hover", function(event) {
if(event.type == "mouseenter") {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
}
else if (event.type == "mouseleave") {
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
}
});
.on
函数只有3个参数:http://api.jquery.com/on/
如果您不需要将处理程序也绑定到动态添加的元素,那么您可以使用具有2个事件处理程序的老hover
函数。
$('.top-level').hover(function (event) {
$(this).find('.actionfcnt').show();
$(this).find('.dropfcnt').show();
}, function (event) {
$(this).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
顺便说一下,$(selector).hover(handlerIn, handlerOut)
是$(selector).mouseenter(handlerIn).mouseleave(handlerOut);
的简写。
如果需要,则对mouseenter
和mouseleave
事件使用on
:
$(document).on('mouseenter', '.top-level', function (event) {
$(this).find('.actionfcnt').show();
$(this).find('.dropfcnt').show();
}).on('mouseleave', '.top-level', function (event) {
$(this).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
尝试
$('.top-level').hover(function (event) {
$( this ).find('.actionfcnt').show();
$( this ).find('.dropfcnt').show();
}, function(){
$( this ).find('.dropfcnt').hide('blind', function(){
$('.actionfcnt').hide();
});
});
相关文章:
- ng-view 中的脚本在从文件引用时不起作用(JQuery 在角度之前加载)
- 转换效果不起作用jquery
- 淡入淡出和AppendTo不起作用-jQuery
- 变量计算不起作用(jquery/javascript)
- 更改事件不起作用 jQuery 自动完成
- 倒计时不起作用 jquery
- ajax 响应包括 image,onClick 事件对图像不起作用 jQuery
- 从JavaScript添加html,但不起作用jQuery切换功能
- 简单的工具提示插件 - 点击选项不起作用 - jQuery
- 图像滑块不起作用(jquery)- Rails 3项目
- 触发单击事件不起作用 jquery
- 动态创建的 li 点击事件不起作用 jQuery
- Jquery验证不起作用.Jquery-Javascript
- JavaScript生成的元素不起作用-jQuery
- 事件委派不起作用jquery
- 类选择器在追加后不起作用 (JQuery)
- 为什么这个变量不起作用?Jquery
- 首先,渐变有时似乎不起作用(jquery)
- 有什么原因吗('img')加载不起作用(jQuery)
- 更改不起作用jQuery