[英]How to call a function with jquery blur UNLESS clicking on a link?
我有一個小的jQuery腳本:
$('.field').blur(function() {
$(this).next().children().hide();
});
隱藏的子項包含一些鏈接。 這樣就無法單擊鏈接(因為它們被隱藏了)。 有什么合適的解決方案?
這與我所獲得的接近:
$('.field').blur(function() {
$('*').not('.adress').click(function(e) {
foo = $(this).data('events').click;
if(foo.length <= 1) {
// $(this).next('.spacer').children().removeClass("visible");
}
$(this).unbind(e);
});
});
假定未注釋的行是指模糊的字段,但它似乎不起作用。 有什么建議么?
您可以稍微延遲一下,如下所示:
$('.field').blur(function() {
var kids = $(this).next().children();
setTimeout(function() { kids.hide(); }, 10);
});
這使您有時間單擊,然后這些子鏈接消失。
這就是我最終這樣做的方式:
var curFocus;
$(document).delegate('*','mousedown', function(){
if ((this != curFocus) && // don't bother if this was the previous active element
($(curFocus).is('.field')) && // if it was a .field that was blurred
!($(this).is('.adress'))
) {
$('.' + $(curFocus).attr("id")).removeClass("visible"); // take action based on the blurred element
}
curFocus = this; // log the newly focussed element for the next event
});
我相信您可以在這種情況下使用.not('a'):
$('.field').not('a').blur(function() {
$(this).next().children().hide();
});
這未經測試,所以我不確定這是否行得通。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.