2 つの要素にフォーカスがないかどうかを確認するにはどうすればよいですか?
テキストボックスとドロップダウンリストがあります。どちらもフォーカスされていない場合は、ドロップダウンを非表示にしたいと考えています。
以下のコードで確認できますか?
if (!$("#dropdown,#textbox").is(":focus"))
2 つの要素にフォーカスがないかどうかを確認するにはどうすればよいですか?
テキストボックスとドロップダウンリストがあります。どちらもフォーカスされていない場合は、ドロップダウンを非表示にしたいと考えています。
以下のコードで確認できますか?
if (!$("#dropdown,#textbox").is(":focus"))
if ($("#dropdown,#textbox").is(":focus")) //its valid it will validate any
{
$("#dropdown").show();
}else{
$("#dropdown").hide();
}
document.activeElement will return the currently focused element, then all you have to do is check that the ID does not match dropdown or textbox, no jQuery needed for this!
var focused = document.activeElement.id;
if ( focused != 'dropdown' && focused != 'textbox') {
document.getElementById('dropdown').style.display = 'none';
}
or jQuery version:
$('#dropdown').toggle( !$("#dropdown, #textbox").is(":focus") );
試す:
if ( !$('#dropdown').is(':focus') && !$("#textbox").is(':focus') ) { your function here }