0

我有这个设置来为我的每个 DIV 添加一个mouseenter事件:

  $(document).ready(function () {
  $('div[id^=domainEnter]').mouseenter(function () {


                toggleSearchDisplay($(this).attr('domaincount'), 'show');

            });
 });

然后我定义了这个函数:

  function toggleSearchDisplay(num, showhide) {

            if ($('div[id=domainDiv' + num + ']').css('display') == "block" || showhide == "hide") {
                $('div[id=domainDiv' + num + ']').hide('fast');
                $('a[id = domainLink' + num + ']').text('+');
                $('input[id ^= SearchControls_' + num + '__SearchControlVisible]').val('false');
            } else {

                $('div[id=domainDiv' + num + ']').show('fast');
                $('a[id = domainLink' + num + ']').text('-');
                $('input[id ^= SearchControls_' + num + '__SearchControlVisible]').val('true');


            }
        }

这一切都很好,并且可以满足我的需要,但是现在我正在尝试在第一个块中的 MouseEnter 位上设置超时/延迟...我尝试了这个,但它永远不会执行:

$('div[id^=domainEnter]').mouseenter(setTimeout(function () {

                toggleSearchDisplay($(this).attr('domaincount'), 'show');

            },1000));

然后我尝试了这个,它执行,但没有延迟......它运行正常:

 $('div[id^=domainEnter]').mouseenter(function () {

            setTimeout(toggleSearchDisplay($(this).attr('domaincount'), 'show'),1000);

        });

我不知道该怎么办......有什么想法吗?

4

1 回答 1

1

In your first try, you're not giving a function as argument to mouseenter but the result of setTimeout, that is a timer.

In the second one, when the callback provided to setTimeout is executed, this is the window.

Here's a way to fix it :

$('div[id^=domainEnter]').mouseenter(function(){
     var $this = $(this);
     setTimeout(function () {
        toggleSearchDisplay($this.attr('domaincount'), 'show');
     },1000)
});
于 2012-09-10T19:18:14.213 回答