单击按钮时,我将类名设置为div标签。我想在外面点击时删除这个类。我该怎么办?我尝试了一些方法,但它不起作用,这是我的尝试:
HTML
<div class="search-box"></div>
<input type="search" name="search" placeholder="search in here" id="search-input" autofocus="autofocus" style="display: none; ">
的jQuery
$('.search-box').click(function() {
$('#search-input').toggle();
})
$("body").not('.search-box').click(function() {
$('#search-input').hide();
})
答案 0 :(得分:1)
假设您需要在点击div时显示文本框,并希望在点击外部时隐藏它。
$('.search-box').click(function(e) {
$('#search-input').toggle();
e.stopPropagation();
})
$('#search-input').click(function(e) {
e.stopPropagation();
})
$(document).click(function(e) {
$('#search-input').hide();
e.stopPropagation();
})
&#13;
.search-box {
background-color: #ddd;
height: 20px;
}
&#13;
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<div class="search-box">
<input type="search" name="search" placeholder="search in here" id="search-input" autofocus="autofocus" style="display: none; ">
</div>
&#13;
答案 1 :(得分:0)
问题解释:问题很清楚。使用这种方法,当用户尝试单击搜索框时,它将隐藏它。用户正试图阻止它。
我更喜欢Bootstrap使用的方式。您的HTML没有任何意义,所以我使用自己的HTML:
$(function () {
$(".search-box").click(function () {
$("body").addClass("search-open");
});
$(".search-mask").click(function () {
$("body").removeClass("search-open");
});
});
.search-box {
padding: 5px;
position: relative;
background: #ccf;
width: 175px;
height: 25px;
cursor: pointer;
}
.search-mask {
display: none;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 2;
}
.search-box input {
position: absolute;
z-index: 3;
display: none;
}
.search-open input,
.search-open .search-mask {
display: block;
}
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<div class="search-mask"></div>
<div class="search-box">
<input type="text" />
</div>
<强>解释强>
我还在这里写了一篇关于同一主题的文章:Evolution of Drop Down Menus and Exiting Them。看看它,你将能够了解这种方法比其他方法更好。
答案 2 :(得分:0)
您可以使用e.stopImmediatePropagation();要停止在外部元素和body元素上触发事件,请调用removeClass()方法,以便在单击div外部时删除所需的类。
像这样:
$(function(){
$(".inner-div").click(function(e){
e.stopImmediatePropagation();
$(".inner-div").addClass("my-class");
});
$("body").click(function(e){
e.stopImmediatePropagation();
$(".inner-div").removeClass("my-class");
});
})
.outer-div{
background :#f00;
width:100%;
height:500px;
}
.inner-div {
background : #ccc;
width :200px;
height:140px;
}
.my-class{
background :#00f;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="outer-div">
outer div
<div class="inner-div">
inner div
</div>
</div>
$(function(){
$('.search-box').click(function(e) {
e.stopImmediatePropagation();
$('#search-input').show();
});
$("body").click(function() {
$('#search-input').hide();
})
});
.search-box {
background-color: #ddd;
height: 20px;
}
.outer-div{
width:100%;
height:400px;
}
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<div class="outer-div">
<div class="search-box">
<input type="search" name="search" placeholder="search in here" id="search-input" autofocus="autofocus" style="display: none; ">
</div>
</div>