我知道很多类似的问题也在Stackoverflow中。这是一个Jquery问题。我添加了toggle类Jquery函数,并且当单击body上的任何位置时应该删除相同的类。它现在正在工作,但是当我们点击输入文本字段时,我不需要切换该类。
代码:
var removeClass = true;
$(".btn-search").click(function() {
$(".input-search").toggleClass('expanded');
removeClass = false;
});
$("html").click(function() {
if (removeClass) {
$(".input-search").removeClass('expanded');
}
removeClass = true;
});
body {
background: #eee;
}
.col-search {
padding: 13px 8px 8px 0;
position: relative;
width: 80%;
}
.input-search {
width: 220px;
height: 34px;
max-width: 0;
padding: 5px 10px;
transition: all .2s ease;
position: absolute;
top: 13px;
bottom: 13px;
right: 46px;
border: 0;
box-sizing: border-box;
opacity: 0;
}
.input-search.expanded {
max-width: 220px;
opacity: 1;
}
.btn-search {
position: absolute;
right: 0;
width: 38px;
height: 34px;
border: 0;
background: #333;
color:#fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="col-search">
<input class="input-search" type="search" placeholder="Search" />
<input type="button" class="btn-search" value="click">
</div>
答案 0 :(得分:7)
要解决此问题,您可以检查哪个元素引发了冒充DOM的click事件。如果它不在.col-search
范围内,那么您可以切换课程。
另请注意,此逻辑会使您的removeClass
变量变得多余。
$(".btn-search").click(function(e) {
e.stopPropagation();
$(".input-search").toggleClass('expanded');
});
$("html").click(function(e) {
if ($(e.target).closest('.col-search').length == 0)
$(".input-search").removeClass('expanded');
});
&#13;
body {
background: #eee;
}
.col-search {
padding: 13px 8px 8px 0;
position: relative;
width: 80%;
}
.input-search {
width: 220px;
height: 34px;
max-width: 0;
padding: 5px 10px;
transition: all .2s ease;
position: absolute;
top: 13px;
bottom: 13px;
right: 46px;
border: 0;
box-sizing: border-box;
opacity: 0;
}
.input-search.expanded {
max-width: 220px;
opacity: 1;
}
.btn-search {
position: absolute;
right: 0;
width: 38px;
height: 34px;
border: 0;
background: #333;
color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="col-search">
<input class="input-search" type="search" placeholder="Search" />
<input type="button" class="btn-search" value="click">
</div>
&#13;
答案 1 :(得分:3)
请务必使用event.target
检查您要排除的输入是否触发了事件这也不会干扰可能与输入本身绑定的其他事件。
var removeClass = true;
$(".btn-search").click(function() {
$(".input-search").toggleClass('expanded');
removeClass = false;
});
$("html").click(function() {
var searchInput = $(".input-search");
if (removeClass && event.target != searchInput[0]) {
searchInput.removeClass('expanded');
}
removeClass = true;
});
&#13;
body {
background: #eee;
}
.col-search {
padding: 13px 8px 8px 0;
position: relative;
width: 80%;
}
.input-search {
width: 220px;
height: 34px;
max-width: 0;
padding: 5px 10px;
transition: all .2s ease;
position: absolute;
top: 13px;
bottom: 13px;
right: 46px;
border: 0;
box-sizing: border-box;
opacity: 0;
}
.input-search.expanded {
max-width: 220px;
opacity: 1;
}
.btn-search {
position: absolute;
right: 0;
width: 38px;
height: 34px;
border: 0;
background: #333;
color:#fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="col-search">
<input class="input-search" type="search" placeholder="Search" />
<input type="button" class="btn-search" value="click">
</div>
&#13;
答案 2 :(得分:1)
只需使用返回值更改输入事件的JS https://jsfiddle.net/fcdo7kyh/
var removeClass = true;
$(".btn-search").click(function() {
$(".input-search").toggleClass('expanded');
removeClass = false;
});
$(".input-search").click(function() {
return false;
});
$("html").click(function() {
if (removeClass) {
$(".input-search").removeClass('expanded');
}
removeClass = true;
});
答案 3 :(得分:1)
请检查以下代码:
HTML:
<input class="input-search" type="search" placeholder="Search" id="inputSearch" />
JS:
$("#inputSearch").click(function(e) {
e.stopPropagation();
});
var removeClass = true;
$(".btn-search").click(function() {
$(".input-search").toggleClass('expanded');
removeClass = false;
});
$("html").click(function() {
if (removeClass) {
$(".input-search").removeClass('expanded');
}
removeClass = true;
});
$("#inputSearch").click(function(e) {
e.stopPropagation();
});
&#13;
body {
background: #eee;
}
.col-search {
padding: 13px 8px 8px 0;
position: relative;
width: 80%;
}
.input-search {
width: 220px;
height: 34px;
max-width: 0;
padding: 5px 10px;
transition: all .2s ease;
position: absolute;
top: 13px;
bottom: 13px;
right: 46px;
border: 0;
box-sizing: border-box;
opacity: 0;
}
.input-search.expanded {
max-width: 220px;
opacity: 1;
}
.btn-search {
position: absolute;
right: 0;
width: 38px;
height: 34px;
border: 0;
background: #333;
color:#fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<div class="col-search">
<input class="input-search" type="search" placeholder="Search" id="inputSearch" />
<input type="button" class="btn-search" value="click">
</div>
&#13;
这是你期望的吗?
答案 4 :(得分:-1)
$(".btn-search").click(function(event) {
event.stopPropagation();
$(".input-search").toggleClass('expanded');
});
$("html").click(function() {
$(".input-search").removeClass('expanded');
});
$(".input-search").click(function(event) {
event.stopPropagation();
$(".input-search").addClass('expanded');
});
body {
background: #eee;
}
.col-search {
padding: 13px 8px 8px 0;
position: relative;
width: 80%;
}
.input-search {
width: 220px;
height: 34px;
max-width: 0;
padding: 5px 10px;
transition: all .2s ease;
position: absolute;
top: 13px;
bottom: 13px;
right: 46px;
border: 0;
box-sizing: border-box;
opacity: 0;
}
.input-search.expanded {
max-width: 220px;
opacity: 1;
z-index: 10;
}
.btn-search {
position: absolute;
right: 0;
width: 38px;
height: 34px;
border: 0;
background: #333;
color:#fff;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="col-search">
<input class="input-search" type="search" placeholder="Search" />
<input type="button" class="btn-search" value="click">
</div>