2013-06-21 18 views
0

我有一個文本輸入,當它有焦點時,在body的任何位置註冊點擊事件。但是當焦點從中刪除時,該點擊事件將從body中刪除。可悲的是,我似乎無法接受它。當輸入焦點時將點擊事件添加到正文,然後在模糊時刪除它

$(document).ready(function() { 

    $("html").on("focus", "#asdf", function() { 
     $("body").on("click", "*:not(#asdf)", wasItClicked); 
    }); 

    $("html").on("blur", "#asdf", function() { 
     $("body").off("click", "*", wasItClicked); 
    }); 

}); 

function wasItClicked() { 
    alert("yeah"); 
} 

BIN

感謝您的幫助。

回答

0

你可以使用的setTimeout刪除點擊使用命名空間添加和刪除事件時,因爲您可能會意外刪除其它click處理程序,但最簡單的方法是刪除處理程序的Click事件:

... 
    $("body").on("click.fromasf", "*:not(#asdf)", wasItClicked); 
    ... 
    function wasItClicked() { 
     $("body").off("click.fromasf"); 
     console.log("yeah"); 
    } 

下面是一個例子使用的超時:

<!DOCTYPE html> 
<html> 
<head> 
<script src="jquery-1.9.0.js"></script> 
<style> 
</style> 
<meta content="text/html; charset=UTF-8" http-equiv="content-type"> 
<title>Example</title> 
</head> 
<body> 
    <input type="text" id="asdf" /> 
    <input type="text" id="Text1" /> 
    <script> 
     $(document).ready(function() { 

      $("html").on("focus", "#asdf", function() { 
       console.log("adding click handler"); 
       $("body").on("click.fromasf", "*:not(#asdf)", wasItClicked); 
      }); 

      $("html").on("blur", "#asdf", function() { 
       setTimeout(function() { 
        console.log("remove click"); 
        $("body").off("click.fromasf"); 
       }, 500); 
      }); 

     }); 

     function wasItClicked() { 
      console.log("yeah"); 
     } 
    </script> 
</body> 
</html> 
1

當#asdf集中,且被點擊其他一些元素,事件觸發以鼠標按下,模糊,鼠標鬆開,請單擊。所以處理程序在點擊之前已經被移除。

mousedown事件在模糊之前觸發。如果你是用鼠標按下,而不是點擊OK,你可以這樣做:

$(document).ready(function() { 

    $("#asdf").on("focus", function() { 
     $("body").on("mousedown", wasItClicked); 
    }); 

    $("#asdf").on("blur", function() { 
     $("body").off("mousedown", wasItClicked); 
    }); 

}); 

(bin)

編輯:

你可以使用鼠標按下事件,以幫助確定,如果你正在失去,因爲關注的焦點點擊並刪除點擊處理程序中的處理程序(如果失去焦點)。

$(document).ready(function() { 
    $("#asdf").on("focus",function() { 
    $("body").on("mousedown", setDown); 
    $("body").on("click", wasItClicked); 
    }); 
    $("#asdf").on("blur", function() { 
    if ($(this).attr("mouse") != "down") { 
    $("body").off("mousedown", setDown); 
    $("body").off("click", wasItClicked); 
    } 
    }); 
}); 

function setDown() { 
    $("#asdf").attr("mouse","down"); 
} 

function wasItClicked() { 
    if ($("#asdf") != $(document.activeElement)) { 
    $("body").off("mousedown", setDown); 
    $("body").off("click", wasItClicked); 
    } 
    $("#asdf").attr("mouse","up"); 
    alert("yeah"); 
} 

new bin

+0

但我想註冊單擊事件。 mousedown怎麼能提供幫助? – 1252748

+0

mousedown可以幫助您確定何時刪除點擊處理程序,因此當您因點擊而失去焦點時實際調用wasItClicked。 (見編輯) – Joe

0

好吧,我看到一對夫婦的問題...

  1. 你在 單個元素委託一個焦點事件的HTML元素的事件.. 。這有點矯枉過正,所以我會將 focusblur事件直接放在input
  2. 當您撥打off,你需要通過它完全一樣的選擇在第二個參數
  3. 點擊事件委託給身體,射擊被點擊並選擇相匹配的任何子元素 - 這不包括body本身.. 。不知道你是否想這樣,但我把它移動到html元素,包括body
  4. 只要input失去焦點,該事件將被刪除,所以點擊將不會註冊(你可以使用超時值@HMR在他們的回答中建議)

我在html元件上遇到了一些問題T爲仍返回輸入(儘管:not(#asdf)選擇),所以我只是把過濾器進入功能。

下面是修改後的代碼(測試版):

var click_selector = ":not(#asdf)"; 
var click_target = 'html'; 

$(document).ready(function() { 

    $("#asdf").on("focus", function() { 
     $(click_target).on("click", click_selector, wasItClicked); 
    }); 

    $("#asdf").on("blur", function() { 
     // Use timeout to be able to register the click before function is removed 
     // NOTE that since 'click' fires when the mouse is released, if they 
     // hold the mouse down for a while, the event will be gone and won't 
     // register. Maybe better to use 'mousedown' instead of 'click' 
     // in which case the timeout could probably be reduced to 10ms or something 

     // Also, using timeouts creates the possibility of multiple click handlers 
     // present at the same time (new one added before the previous is removed) 

     setTimeout(function(){ 
      $(click_target).off("click", click_selector, wasItClicked); 
     }, 100); 
    }); 

}); 

function wasItClicked(e) { 
    e.stopPropagation(); 
    e.preventDefault(); 

    if(e.target.id !== 'asdf'){ 
    console.log('yeah', click_target, click_selector); 
    } 
} 
相關問題