当用户点击/点击它之外时,使搜索栏消失

时间:2016-12-01 12:27:38

标签: javascript jquery html

我是一个全新的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标记是相同的,但我添加了一个搜索表单,而不是下拉菜单。

谢谢!

3 个答案:

答案 0 :(得分:0)

这段代码会删除每个没有dropbtn课程的div中的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类添加到搜索框元素中,或将其更改为已有的类。

答案 1 :(得分:0)

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');
      }
    }
  }
}

答案 2 :(得分:0)

以上都没有为我工作,所以我最终自己组建了一个解决方案。我用下面的jQuery替换了javascript。

当用户点击搜索图标时,这是在slideUp和slideDown之间切换:

$( "#mobile-search-ico" ).click(function() {
  $( ".mobile-search-inner" ).slideToggle(400);
});

这是在用户点击搜索框外部时切换slideUp:

$(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”取代了搜索图标(不是按钮)。另外,造型有点不同。

相关问题