ユーザーがハイパーリンクの上にマウスを置いたときに表示されるポップアップ検索ボックスがあります。ユーザーが検索ボックスからマウスを離すと、ボックスが消えます。これはうまくいきます。テキストボックスにフォーカスがある場合、検索ボックスは、テキストボックスがフォーカスを失うまで表示されたままになるはずです。フォーカスが失われた時点で、カーソルがボックスの上にない場合、ボックスは非表示になります。これは、IE (具体的には IE7) を除くすべてのブラウザーでうまく機能します。IE では、テキスト ボックスのマウスアウト時にテキスト ボックスのブラー イベントが発生し、検索ボックスが効果的に非表示になります。ここに私が書いたコードがあります:
<script type="text/javascript">
$(document).ready(function() {
//add mouseover event to the search button to show the search box
$(".search").mouseover(
function() {
$(".open").show();
});
//add mouseout event to the search button to show the hide box
$(".search").mouseout(
function() {
$(".open").hide();
});
//add mouseover event to the search box so it doesnt hide when the user attempts to click the box
$(".open").mouseover(
function() {
$(".open").show();
});
//add mouseout event to the search box so it doesnt hides when the users mouse exits the box
$(".open").mouseout(
function() {
$(".open").hide();
});
//don't ever hide the search box when the textbox has focus
$("#tbSearch").focus(
function() {
$(".open").mouseout(
function() {
$(".open").show();
});
});
//hide the search box when the textbox loses focus
$("#tbSearch").blur(
function() {
$(".open").hide();
$(".open").mouseout(
function() {
$(".open").hide();
});
});
});
</script>
HTML は次のとおりです。
<a class="search" href="#"><span>Search</span></a>
<div class="open">
<input id="tbSearch" type="text" />
<a class="go" href="#"><span>Go</span></a>
</div>