一旦鼠标不再存在,就隐藏按钮内容

时间:2018-09-05 01:15:54

标签: javascript html css state

当鼠标不再悬停在元素上方时,我一直试图使搜索栏消失。我认为这是因为它是一个按钮,而不是li,但是如果可能的话,我想两者都做。 enter image description here

    <div class="dropdown">
     <button onclick="regionSearch()" class="dropbtn">search by Region</button>
       <div id="regionSearch" class="dropdown-content">
         <input type="text" placeholder="Search.." id="regionInput" 
          onkeyup="regionFilter()">
      <a href="north-island/bop/bop.html">Bay of Plenty</a>
      <a href="north-island/auckland/auckland.html">Auckland</a>
      <a href="north-island/northland/northland.html">Northland</a>
      <a href="north-island/waikato/waikato">Waikato</a>
      <a href="north-island/gisborne/gisborne">Gisborne</a>
      <a href="north-island/hawkesbay/hawkesbay.html">Hawkes Bay</a>
      <a href="north-island/wellington/wellington.html">Wellington</a>
      <a href="north-island/taranaki/taranaki.html">Taranaki</a>
      <a href="north-island/manawatu/manawatu.html">Manawatu</a>
      <a href="south-island/tasmon/tasmon.html">Tasmon</a>
      <a href="south-island/nelson/nelson.html">Nelson</a>
      <a href="south-island/malborough/malborough.htm;">Malborough</a>
      <a href="south-island/westcoast.westcoast.html">West Coast</a>
      <a href="south-island/canterbury/canterbury.html">Canterbury</a>
      <a href="south-island/otago/otago.html">Otago</a>
  </div>
</div>

              function citySearch() {
    document.getElementById("citySearch").classList.toggle("show");
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("citySearch");
    a = div.getElementsByTagName("a");
    for (i = 0; i < a.length; i++) {
        if (a[i].innerHTML.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}

我有一个以上的样式是这样。

#myInput,#regionInput,#diffInput:focus {
  outline: 3px solid #ddd;
}

.dropdown {
    position: relative;
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    max-height: 100px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
    overflow-y: scroll;
}

.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}

所有这些代码都可以正常工作,并且外观和感觉已经过时了。

2 个答案:

答案 0 :(得分:0)

我会发表评论,如果我不能回答一个完整的答案,尽管这可能是您的解决方案-您没有没有使用JS来实现这种效果。

就像您在CSS文件的下拉菜单中使用的一样,您可以

#searchbox{ display: none; } /* default is not displayed / no mouse hover
#searchbox:hover{ display: inline-block; } /* upon mouse hover, show the search

要补充一点,我曾经有过像您一样的感觉。我试图从头开始制作一个完整的javascript下拉式悬停菜单,但实际上需要两行CSS代码。

答案 1 :(得分:0)

感谢您zatyh将按钮更改为div的想法,因为它启发了我在html中执行以下操作

<div onmouseover="diffSearch()" onclick="diffSearch()" 
 onmouseout="hideDiff()"class="dropbtn">search by Difficulty</div>

//并将hideDiff函数添加到我的javascript文件中

function hideDiff(){ x=document.getElementByid("diffSearch"); x.style.display("none");}

这行得通,但仍然存在一些错误