Jquery Bind,Unbind

时间:2011-07-14 10:02:54

标签: jquery jquery-click-event

我将一些点击事件绑定到我拥有的某些图像上。当点击其中一个图像时,我想禁用所有图像,然后在两秒后添加相同的点击事件。

到目前为止,我有以下内容:

 $('#pagination img').each(function(index){
        $( this ).bind ("click",function(){
               pageClick = true;
               pagendex = index*9;
           clearInterval(progresstracker);

             $("#progressbar").progressbar( "option", "value", 0);
             numSecondsPassed=0;
             $('#pagination img').each(function(index){
                $(this).unbind('click');

             });

            // alert("Page Index" + pageIndex);
              rotateImage();

             }).mouseover(function(){
                  $(this).css("cursor","pointer");
            });
        });

我正在解除它们的绑定并且有效。但是我需要在两秒钟之后将它们绑定回具有相同变量的click事件并再次解除绑定。基本上当它们单击解除绑定2秒然后绑定回冲洗重复。

2 个答案:

答案 0 :(得分:0)

您可以使用setTimeout()在取消绑定后2秒重新安排绑定。这可能意味着提取绑定/解除绑定的功能。


从我的头脑中,我想的是:

var bindAll = function () {
  $("...").each(function (index, item) {
     item.bind("click", function (event) {
          // ... do your work 
          unbindAll();
     });
};

var undbindAll = function () {
     $("...").unbind('click');
     setTimeout(bindAll, 2000);
} 

现在假设你要解开所有图像的事件,我明白了吗?对不起,如果我错过了某些内容,或者这在你的情况下不起作用......

答案 1 :(得分:0)

我不会解除绑定,因为你可以创建全局变量,跟踪,如果你可以点击按钮或不喜欢这样:

<html>
<head>
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
    var buttonClickable = true;
$(document).ready(function() {
    $(".button").click(function() {
        if (buttonClickable)
        {
            $("body").append($(this).text());
            buttonClickable = false;
            setTimeout("buttonClickable = true; alert('passed');",2000);
        }
        else
        {

        }
    });
});
</script>
</head>
<body>
<div class="button">Button1</div>
<div class="button">Button2</div>
</body>
</html>
相关问题