Javascript Jquery mouseleave setTimeout don';行不通
我在jQuery中有以下代码:Javascript Jquery mouseleave setTimeout don';行不通,javascript,jquery,html,Javascript,Jquery,Html,我在jQuery中有以下代码: $( ".name" ) .on( "mouseenter", function() { $(this).find("ul").css({"font-size": "20px", 'color': "red"}); }) .on( "mouseleave", function() { setTimeout(function () { $(this).find("ul").css({"font-size": "12p
$( ".name" )
.on( "mouseenter", function() {
$(this).find("ul").css({"font-size": "20px",
'color': "red"});
})
.on( "mouseleave", function() {
setTimeout(function () {
$(this).find("ul").css({"font-size": "12px",
'color': "blue"});
}, 5000);
});
第一部分正在工作,但第二部分已损坏
为什么
mouseleave
上的setTimeout
不起作用?请尝试将事件作为参数传递,并使用$(event.target)代替$(this),尝试将事件作为参数传递,并使用$(event.target)这是一个范围问题$(此)
设置超时内的不引用$('.name')
。您可以通过将this
的变量设置为正确的级别并引用该变量来解决此问题
.on("mouseleave", function() {
var self = $(this);
setTimeout(function() {
self.find("ul").css({
"font-size": "12px",
'color': "blue"
});
}, 5000);
});
这是一个范围界定问题$(此)
设置超时内的不引用$('.name')
。您可以通过将this
的变量设置为正确的级别并引用该变量来解决此问题
.on("mouseleave", function() {
var self = $(this);
setTimeout(function() {
self.find("ul").css({
"font-size": "12px",
'color': "blue"
});
}, 5000);
});
此
解析为错误的内容。此
解析为错误的内容。