当用户点击/点击以外时,使搜索栏消失
我是一个总的JavaScript新手。我试图创建一个搜索栏,当用户点击我已经添加到顶栏的搜索图标时出现。我用这个tutorial当用户点击/点击以外时,使搜索栏消失
我调整了HTML标记以满足我的需要,但是,当我添加下面的代码(与本教程相同)时,某些东西似乎出错了。
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
当用户点击图标时会出现搜索栏,但如果我尝试选择搜索字段来写东西,它会消失。我检查了一下,发现上面的教程有相同的行为(如果你点击下拉菜单中的其中一个链接,下拉菜单就会消失)。
我知道它与事件目标有关。有谁知道我应该改变什么以及在哪里? HTML标记是相同的,除了下拉菜单,我添加了一个搜索表单。
谢谢!
这段代码删除每个div中没有dropbtn
类的show class。
你可以做这样的事情
if (!event.target.matches('.dropbtn') && !event.target.matches('.searchBox')) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
,并添加.searchBox
类搜索框元素,或更改它已经拥有的类。
event.target.matches
不起作用。使用这个fiddle。愿它帮助!
JS:
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
if (event.target.className.indexOf('dropbtn') == -1) {
var dropdowns = document.getElementsByClassName("dropdown-content");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
我想我可能会错过一些东西,因为当您点击其中的某个链接时,下拉列表不会保持可见状态。 –
以上都不为我工作,所以我最终组建一个自己的解决方案。我用下面的jQuery替换了javascript。
这是当用户点击搜索图标效果基本show和了slideDown之间切换:
$("#mobile-search-ico").click(function() {
$(".mobile-search-inner").slideToggle(400);
});
这是为了当用户点击搜索框的外侧切换效果基本show:
$(document).mouseup(function (e)
{
var container = $(".mobile-search-inner");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.slideUp(400);
}
});
$("#mobile-search-ico").click(function() {
$(".mobile-search-inner").slideToggle(400);
});
$(document).mouseup(function(e) {
var container = $(".mobile-search-inner");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.slideUp(400);
}
});
form#menu-mobile-search-main {
width: 97.6%;
margin-right: auto;
margin-left: auto;
}
input.menu-mobile-searchform-main {
border: 1px solid #000 !important;
background: #fff !important;
font-family: "classico-urw" !important;
color: #000 !important;
font-size: 13px !important;
height: 37.33px !important;
padding: 10px !important;
padding-right: 47px !important;
margin: 0 !important;
}
input.menu-mobile-searchform-main:focus,
button.mobile-search-submit-main:focus {
outline: none !important;
}
button.mobile-search-submit-main {
background: #fff;
border: none;
padding: 0px;
position: absolute;
top: 15.5px;
right: 20px;
}
i.mobile-search-submit-icon {
font-family: 'cj-icons' !important;
font-size: 17px;
speak: none;
font-style: normal;
font-weight: normal;
font-variant: normal;
text-transform: none;
display: inline-block;
line-height: 1;
letter-spacing: 0;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: "liga";
-moz-font-feature-settings: "liga=1";
-moz-font-feature-settings: "liga";
-ms-font-feature-settings: "liga" 1;
font-feature-settings: "liga";
-webkit-font-variant-ligatures: discretionary-ligatures;
font-variant-ligatures: discretionary-ligatures;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
color: #000;
cursor: pointer;
}
.mobile-search-inner {
position: fixed;
left: 0px;
width: 100%;
top: 49px;
right: auto;
background: #fff;
height: 47.5px;
padding-top: 5.083px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div class="mobile-search-relative">
<div id="mobile-search" class="mobile-search"><i id="mobile-search-ico" class="mobile-search-ico">ICON</i>
<div id="mobile-search-show" class="mobile-search-inner">
<div class="mobile-search-hide">
<form id="menu-mobile-search-main" role="search" method="get" action="<?php echo home_url('/'); ?>">
<input type="text" value="" name="s" placeholder="" class="menu-mobile-searchform-main">
<button type="submit" rel="nofollow" class="mobile-search-submit-main"><i class="fa fa-search"></i>
</button>
</form>
</div>
</div>
</div>
</div>
注意:切换搜索框的搜索图标是使用linea图标字体创建的,搜索按钮是使用FontAwesome创建的。我不确定如何在这里添加这些内容,所以我将它们排除在外。 “ICON”取代搜索图标(不是按钮)。此外,造型有点不同。
可能的重复[使用jQuery隐藏DIV,当用户点击它之外](http://*.com/questions/1403615/use-jquery-to-hide-a-div-when-the-用户点击 - 它之外) –
它在教程中工作正常。检查这[[小提琴](https://jsfiddle.net/pandeyvishal1986/y598xd5d/#&togetherjs=a0mpQAwpVc) – RonyLoud
你的标题是误导性的,因为你试图做相反的事情,你的搜索框是什么类? –