带有setTimeout的jQuery live函数

带有setTimeout的jQuery live函数,jquery,live,settimeout,mouseenter,Jquery,Live,Settimeout,Mouseenter,我正在尝试将mouseenter、live和setTimeout函数结合起来制作一些动画 $(".kundenList li").live("mouseenter", function(){ if ($(this).children().length > 0) { $(this).data('timeout', setTimeout( function () { $(this).children("div.description").css(

我正在尝试将mouseenter、live和setTimeout函数结合起来制作一些动画

$(".kundenList li").live("mouseenter", function(){
    if ($(this).children().length > 0) {
        $(this).data('timeout', setTimeout( function () {
            $(this).children("div.description").css({opacity: '0'});

            $(this).children("div.blacklayer").animate({
                opacity: '0.85'
            }, 300);

            $(this).children("div.description").animate({
                top: "-=200px",
                opacity: '1'
            }, 300).css( 'cursor', 'pointer' );

            $(this).addClass("activeLI");
        }, 300));
    }
});
Html看起来像这样

<ul class="kundenList">
    <li>
        <img src="t3.png" class="kundenImg" />
        <div class="blacklayer" style="opacity: 0;"></div>
        <div class="description">
            <h3>Title</h3>
            <p>Description</p>
        </div>
    </li>
</ul>
  • 标题 描述

因为我张贴的问题纸条显然不起作用:)。有人知道为什么吗?谢谢


PS我需要live功能,因为我正在通过ajax加载新内容

内部的
设置超时
指的是全局
窗口
对象。将对该的引用存储在临时变量中,如下所示小提琴:


内部的
设置超时
指的是全局
窗口
对象。将对该的引用存储在临时变量中,如下所示小提琴:


您不能使用
内部超时功能访问
li
。在超时函数定义变量之前,将
$(this)
分配给它,并在函数中使用它,如()所示:


您不能使用
内部超时功能访问
li
。在超时函数定义变量之前,将
$(this)
分配给它,并在函数中使用它,如()所示:


我没有收到任何错误消息。如果我删除setTimeout函数,一切正常。我没有收到任何错误消息。如果我删除setTimeout函数,一切正常。戈尔迪,罗布比我快。请接受他的回答。如果您将光标悬停在回答时间上,您可以看到准确的回答时间,并且他比我快5秒
超时时间
确实有效。它在300毫秒后执行一次函数。您是否将
setTimeout
setInterval
混淆?好的。。。我看到它现在起作用了。问题是,如果mouseenter持续至少300毫秒,我需要执行该函数。我已经在另一篇文章中回答了这个问题。看:戈尔迪,罗布比我快。请接受他的回答。如果您将光标悬停在回答时间上,您可以看到准确的回答时间,并且他比我快5秒
超时时间
确实有效。它在300毫秒后执行一次函数。您是否将
setTimeout
setInterval
混淆?好的。。。我看到它现在起作用了。问题是,如果mouseenter持续至少300毫秒,我需要执行该函数。我已经在另一篇文章中回答了这个问题。看:你检查过这个例子了吗?它起作用了。你添加了其他代码吗?我添加了一些css。在你鼠标移动到elemet后,脚本开始工作300毫秒(现在我改为500毫秒)。现在,当你用鼠标点击elemet时,它会立即工作。300毫秒或500毫秒,太快了,设置为5000以查看它是否工作。对不起,我没有很好地表达我的问题。问题是,如果鼠标指针持续至少300毫秒,我需要执行该函数。您是否检查该示例?它起作用了。你添加了其他代码吗?我添加了一些css。在你鼠标移动到elemet后,脚本开始工作300毫秒(现在我改为500毫秒)。现在,当你用鼠标点击elemet时,它会立即工作。300毫秒或500毫秒,太快了,设置为5000以查看它是否工作。对不起,我没有很好地表达我的问题。问题是,如果mouseenter持续至少300毫秒,我需要执行该函数
$(".kundenList li").live("mouseenter", function(){
    var $this = $(this); //Reference to `$(this)`

    if ($this.children().length > 0) {
        $this.data('timeout', setTimeout( function () {
            // $this points to $(this), as defined previously
            $this.children("div.description").css({opacity: '0'});

            $this.children("div.blacklayer").animate({
                opacity: '0.85'
            }, 300);

            $this.children("div.description").animate({
                top: "-=200px",
                opacity: '1'
            }, 300).css( 'cursor', 'pointer' );

            $this.addClass("activeLI");
        }, 300));
    }
});
$(".kundenList li").live("mouseenter", function(){
    var $this = $(this);
    if ($this.children().size() > 0) {

        $this.data('timeout', setTimeout( function () {
            $this.children("div.description").css({opacity: '0'});

            $this.children("div.blacklayer").animate({
                opacity: '0.85'
            }, 300);

            $this.children("div.description").animate({
                top: "-=200px",
                opacity: '1'
            }, 300).css( 'cursor', 'pointer' );

            $this.addClass("activeLI");
        }, 300));
    }
});