jqueryフォーカスアウトを除いて
-
04-10-2019 - |
質問
ユーザーが別の特定のDIVをクリックしない限り、テキストボックスがフォーカスを失った場合、私は単にDIVを非表示にしたい。ユーザーがその特定のdivをクリックした場合、フォーカスアウトはDiv.boxが非表示になっていることをトリガーしません。
以下は、私の擬似コードのコメントのコードです。何か案は?
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の使用を使用できます 。データ たとえば、フラグ値を保存します 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();
});
他のヒント
$(document).bind('click',function(e) {
if ($(e.target).closest('div.box').length) return;
$('div.box').hide();
});
これを試して!
var flag = false;
$('#inputID').blur(function(){
$('div.box').toggle(flag);
});
$('#someDIVid').click(function(){
flag = true;
});
シンプルなデモ
追加メモ:
.toggle(showorhide)
showorhide 要素を表示するか非表示にするかを示すブール。このパラメーターの場合
true
, 、それから一致した 要素が表示されます;もしもfalse
, 、 要素は隠されています.
所属していません StackOverflow