【问题标题】:JavaScript: Cannot set display to none, because it sets itself to blockJavaScript:无法将显示设置为无,因为它将自身设置为阻止
【发布时间】:2020-04-28 18:38:28
【问题描述】:

我有一个包含项目的下拉菜单,我正在努力做到这一点,以便当你经过某个项目时,第二个项目会出现在它下面,而当你没有将鼠标悬停在其中任何一个项目上时,它就会消失。 问题是,如果您使用它调用的光标再次扩展并且第二个项目不会消失。我该如何解决?

提前致谢! (该项目的显示默认设置为无)

我尝试将这些项目分开,以便第一个不会再次切换第二个,但我没有使用 javascript 这样做。 https://imgur.com/a/HJsULe2

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>

【问题讨论】:

  • document.getElementById(i) 你一遍又一遍地循环元素....
  • 你调用retract但你没有通过i,你也绑定了很多次事件。
  • 您可以通过在 CSS 中使用 :hover 伪选择器来简化此操作并保留您的样式。
  • 但是当我和 crusor 一起下山时它会起作用......
  • 谢谢espascarello。现在我通过了我,但它仍然不起作用。

标签: javascript menu navigation dropdown


【解决方案1】:

尝试在您的 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>

【讨论】:

  • 谢谢!我在这上面浪费了几个小时哈哈。
猜你喜欢
  • 2016-02-15
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-30
  • 1970-01-01
  • 2015-03-13
  • 2016-01-25
  • 1970-01-01
相关资源
最近更新 更多