Javascript setTimeout不能与$一起使用(此)

Javascript setTimeout不能与$一起使用(此),javascript,jquery,html,Javascript,Jquery,Html,$(文档).ready(函数(){ $(“ul>li”).mouseenter(函数(){ setTimeout(函数(){ $(this.find(“.ul2”).show(); }, 2000); }); $(“ul>li”).mouseleave(函数(){ $(this.find(“.ul2”).hide(); }); }); .ul1{ 列表样式:无; 位置:绝对位置; } .ul1>li{ 浮动:左; 左边距:40px; } .ul2{ 显示:无; 边际:0px; 填充:0px; }

$(文档).ready(函数(){
$(“ul>li”).mouseenter(函数(){
setTimeout(函数(){
$(this.find(“.ul2”).show();
}, 2000);
});
$(“ul>li”).mouseleave(函数(){
$(this.find(“.ul2”).hide();
});
});
.ul1{
列表样式:无;
位置:绝对位置;
}
.ul1>li{
浮动:左;
左边距:40px;
}
.ul2{
显示:无;
边际:0px;
填充:0px;
}
.ul2 li{
显示:块;
}

    • 关于
    • 关于
    • 关于
    • 其他
    • 其他
    • 其他

函数内部指的是该函数,而不是“外部”函数的

setTimeout(function () { // you have a new this now
    $(this).find(".ul2").show();
}, /* you have the old this now */ 2000);
您需要在函数之外获取对该
的引用:

var that = this;
setTimeout(function () {
    $(that).find(".ul2").show();
}, 2000);
或将不同的
绑定到函数:

setTimeout(function () {
    $(this).find(".ul2").show();
}.bind(this), 2000);