JavaScript。不能将显示设置为无,因为它自己设置为屏蔽。

问题描述 投票:0回答:1

我有一个下拉菜单,里面有很多项目,我想让它成为一个下拉菜单,当你经过一个项目时,第二个项目就会出现在它的下方,当你不悬停在任何一个项目上时,它就会消失。问题是,如果你用光标向上走,它会再次调用扩展,第二个项目就不会消失。我怎样才能解决这个问题呢?

先谢谢你 (该项目默认设置为无显示)

我试着把这些项目分开,这样第一个项目就不会再切换第二个项目,但我用javascript做不到。https:/imgur.comaHJsULe2

function extend(i) {
    document.getElementById(i).className = "extend";
    document.getElementById(i).style.display = "block";
    document.getElementById(i).style.backgroundColor = "#383838";
    document.getElementById(i).addEventListener("mouseleave",function() { retract(i); });
    return;
}
function retract(i){
    document.getElementById(i).style.display = 'none';
    return;
}

下拉菜单是用html创建的

<div class="dropdown" id="links">
  <button class="dropbtn">Menu</button>
  <div class="dropdown-content">
    <a href="../index.html">StarLink</a>
    <a href="s_page1.html" onmouseover="extend(1);">StarLink Concerns ></a>
    <div class="extend";>
        <a style="display: none;" id="1" onmouseleave="retract(1);" href="sub/sub_page1.html">SpaceX ></a>
    </div>
    <a href="s_page2.html" onmouseover="extend(2);">Second Page></a>
    <div class="extend";>
        <a style="display: none;" id="2" onmouseleave="retract(2);" href="sub/sub_page2.html">Sub Page 2></a>
    </div>
    <a href="s_page3.html" onmouseover="extend(3);">Third Page></a>
    <div class="extend";>
        <a style="display: none;" id="3" onmouseleave="retract(3);" href="sub/sub_page3.html">Sub Page 3></a>
    </div>
    <a href="s_page4.html" onmouseover="extend(4);">Fourth Page></a>
    <div class="extend";>
        <a style="display: none;" id="4" onmouseleave="retract(4);" href="sub/sub_page4.html">Sub Page 4></a>
    </div>
    <a href="s_page5.html">Fifth Page</a>
    <a href="s_page6.html">Sixth Page</a>
    <a href="s_page7.html">Launch Video</a>
  </div>
</div>
javascript menu navigation dropdown
1个回答
0
投票

试着在你的CSS中保留样式。如果你使用得当,CSS是非常强大的。使用 :hover 伪选择器来设置一个项目被悬停时的样式。因为你有一个你想锁定的兄弟元素,所以使用了 "伪选择器"。+ 选择器来选择需要操作的相邻元素。

.dropdown-content a {
  display: block;
  padding: 15px;
  background: #111111;
  color: white;
  text-decoration: none;
}

.dropdown-content .extend a {
  display: none;
  background-color: #383838;
  color: white;
}

.dropdown-content > a:hover + .extend a,
.dropdown-content .extend a:hover{
  display: block;
}
<div class="dropdown" id="links">
  <button class="dropbtn">Menu</button>
  <div class="dropdown-content">
    <a href="../index.html">StarLink</a>
    <a href="s_page1.html">StarLink Concerns ></a>
    <div class="extend">
        <a id="1" href="sub/sub_page1.html">SpaceX ></a>
    </div>
    <a href="s_page2.html">Second Page></a>
    <div class="extend">
        <a id="2" href="sub/sub_page2.html">Sub Page 2></a>
    </div>
    <a href="s_page3.html">Third Page></a>
    <div class="extend">
        <a id="3" href="sub/sub_page3.html">Sub Page 3></a>
    </div>
    <a href="s_page4.html">Fourth Page></a>
    <div class="extend">
        <a id="4" href="sub/sub_page4.html">Sub Page 4></a>
    </div>
    <a href="s_page5.html">Fifth Page</a>
    <a href="s_page6.html">Sixth Page</a>
    <a href="s_page7.html">Launch Video</a>
  </div>
</div>
© www.soinside.com 2019 - 2024. All rights reserved.