在 setTimeout 中运行的函数失去范围



我在正确触发以下函数时遇到问题。一切正常,除了我认为我从 setTimeout 函数内部失去了范围。这是JS:

function galContent(){
    var hovInt;
    $(".element").each(function(){
        $(this).on("mouseenter", function(){
            clearTimeout(hovInt);
            hovInt = setTimeout(function(){
                //following line not working...
                $(this).find(".elContent").slideDown();
            }, 300);
       });
       $(this).on("mouseleave", function(){
           $(this).find(".elContent").slideUp(); 
       });
    });
}

和 HTML:

<div class="element web">
    <div class="elImg">
        <img src="01.jpg" alt="" title="">
    </div>
    <div class="elContent">
   </div>
</div>

如果this引用setTimeout中的另一个对象,则以这种方式传递引用器:

function galContent(){
    var hovInt;
    $(".element").each(function(i){
        $(this).on("mouseenter", function(){
            clearTimeout(hovInt);
            hovInt = setTimeout(function(){
                //following line not working...
                $(".element").eq(i).find(".elContent").slideDown();
            }, 300);
       });
       $(this).on("mouseleave", function(){
           $(".element").eq(i).find(".elContent").slideUp(); 
       });
    });
}

物业index()也可以工作。

最新更新