gpt4 book ai didi

jquery - 单击外部 > 隐藏()

转载 作者:行者123 更新时间:2023-12-03 22:59:03 28 4
gpt4 key购买 nike

我有这个 jquery 代码:

$(document).ready(function(){
//global vars
var searchBoxes = $(".box");
var searchBox = $(".box");
var searchBoxDefault = "Search...";

searchBoxes.focus(function(e){
$(this).addClass("active");
$('#searchoptions').show();
});
searchBoxes.blur(function(e){
$(this).removeClass("active");
});

searchBox.focus(function(){
if($(this).attr("value") == searchBoxDefault) $(this).attr("value", "");
});
searchBox.blur(function(){
if($(this).attr("value") == "") $(this).attr("value", searchBoxDefault);
}); });

和 html 代码:

<div id="search" class="right">
<form method="post" class="clearfix">
<input class="box left" type="text" value="Search..." />
<input class="button right" type="submit" value="" />
</form>
<div id="searchoptions">
Options:<br /><input checked="checked" type="radio"> Option1</label>
<input type="radio"> Option2</label>
<input type="radio"> Option3</label>
</div>
</div>

问题是:当我点击#search id之外时,如何隐藏#searchoptions?

我尝试了 body 点击,但有错误......并且运行不完美......

最佳答案

body 点击应该将会完美地工作。唯一要做的就是停止事件传播。这应该有效...

$('body').click(function() {
$('#searchoptions').hide();
});

$('#searchoptions').click(function(event){
event.stopPropagation();
});

@参见http://api.jquery.com/event.stopPropagation/

关于jquery - 单击外部 > 隐藏(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4902148/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com