我只是想隱藏一個div,如果文本框失去焦點,除非用戶在另一個特定div中點擊。如果用戶單擊該特定的div,則聚焦不會觸發div.box被隱藏。jQuery focusOut除了如果
下面是我的僞代碼評論的代碼。有任何想法嗎?
textInput.focusout(function() {
// So long you didn't click div.stop, then
$('div.box').hide();
});
我只是想隱藏一個div,如果文本框失去焦點,除非用戶在另一個特定div中點擊。如果用戶單擊該特定的div,則聚焦不會觸發div.box被隱藏。jQuery focusOut除了如果
下面是我的僞代碼評論的代碼。有任何想法嗎?
textInput.focusout(function() {
// So long you didn't click div.stop, then
$('div.box').hide();
});
var flag = false;
$('div.stop').click(function() {flag = true;});
textInput.focusout(function() {
// So long you didn't click div.stop, then
if(!flag)
$('div.box').hide();
});
如果你想避免添加的標誌變量,你可以使用jQuery的.data存儲針對例如標誌值在div.stop
元素,如:
$('div.stop').click(function() {$(this).data('clicked', true);});
// ...
if(!$('div.stop').data('clicked'))
// ...
編輯
如果你想允許在文本框中具有焦點的情況,然後你點擊div.stop
,你不希望它隱藏,那麼你可以嘗試這樣的:
$('div.stop').click(function() {$('div.box').stop();});
textInput.focusout(function() {
// So long you didn't click div.stop, then
$('div.box').delay(200).hide();
});
var flag = false;
$('#inputID').blur(function(){
$('div.box').toggle(flag);
});
$('#someDIVid').click(function(){
flag = true;
});
加入的備註:
.toggle(showOrHide)
showOrHide一個布爾指示是否顯示或隱藏的元素。如果此參數是
true
,則匹配的元素顯示爲;如果false
,則隱藏元素。
$(document).bind('click',function(e) {
if ($(e.target).closest('div.box').length) return;
$('div.box').hide();
});
試試這個!
嗯......你測試過了嗎?我認爲文本框可能在點擊按鈕上點擊之前失去焦點。 – Strelok 2010-09-22 06:17:04
@Strelok - 編輯。我認爲你的解釋是正確的。 – sje397 2010-09-22 06:37:09
我做了一些修改,使其能夠在文檔中被多次觸發,但是您提供的一般解決方案是正確的。謝謝! – James 2010-09-22 15:46:29