打开和悬停不一起工作
on and hover not working together
My jquery code它不起作用时
$("a").on("hover",function(){$(this).css("background","#ccc");},function(){$(this).css("background","#fff")});
但是在工作时
$("a").hover(function(){$(this).css("background","#ccc");},function(){$(this).css("background","#fff")});
如何使其与悬停一起使用
如果悬停.on()
它看起来像
$("a").on('hover', function(e) {
if(e.type =='mouseenter') {
// code for mouseenter
} else {
// code for mouseleave
}
});
但是对于.hover()
来说,接受两个函数,第一个用于mouseenter
,第二个用于mouseleave
。
$('a').hover(
// for mouseenter
function() {
},
// for mouseleave
function() {
}
);
因此,如果您想使用.on()
那么您的代码将:
$("a").on('hover', function(e) {
if(e.type =='mouseenter') {
// code for mouseenter
$(this).css("background","#ccc");
} else {
// code for mouseleave
$(this).css("background","#fff")
}
});
如果您想分别绑定mouseenter
和mouseleave
,@ThiefMaster评论,您可以尝试:
$('a')
.mouseenter(function() {
$(this).css('background', '#ccc');
})
.mouseleave(function() {
$(this).css('background', '#fff');
});
或使用您可以执行的操作.on()
$('a').on({
mouseenter: function() {
$(this).css('background', '#ccc');
},
mouseleave: function() {
$(this).css('background', '#fff');
}
});
这是一个现场演示
和代码
$("a").on('hover', function(e) {
if(e.type =='mouseenter') {
// do something when mouse enter
alert("mouse enter");
} else {
// do something when mouse leave
alert("mouse leave");
}
});
悬停是mouseenter
和mouseleave
事件的快捷方式。因此,您可以使用类似方式绑定那些
$("a").on({
mouseenter: function(){$(this).css("background","#ccc");},
mouseleave: function(){$(this).css("background","#fff");}
});
相关文章:
- 如何在php中创建一个函数,该函数与文本区域一起工作,通过输入类似[color:red]的内容来打印具有等效颜色的文本
- document.title函数可以't设置它与php一起工作
- "此网站似乎使用滚动链接定位效果.这可能不能很好地与异步平移一起工作;
- 非常简单的js测试;Don’不要和Minko一起工作
- 无法使我的文本参数与我的查询一起工作
- 为什么Angularjs验证don't与输入[type=“number”]一起工作
- 无法使vash 0.8.0与express 4.12.3一起工作
- 如何使Angular JS控制器与指令一起工作
- 无法使autocompletion与bootstrap和php代码点火器一起工作
- 注入的元素和jQuery脚本.如何让他们一起工作
- 两个独立工作的javascript函数,但不能一起工作
- 角度和砖石一起工作
- Node http-proxy-middleware 不能与本地服务器一起工作
- jQuery脚本不想一起工作
- page.js使examples/hash与hashbang:true一起工作
- javascript使jquery倒计时与UTC时间一起工作
- jQuery.not()选择器无法与类一起工作
- Rails3-可排序列表不能与wysihtml5一起工作
- 替换匹配函数给出了未定义的错误,但它与替换一起工作
- 按钮上的数据加载消息无法与ajax调用一起工作