用于简单菜单扩展的 Javascript

发布于 2024-11-30 07:00:24 字数 783 浏览 1 评论 0原文

您好,我是 JavaScript 新手,我想编写一个非常简单的扩展子菜单。

<div id="submenu">
  <ul>
   <li>
     Something
   </li>
   <li>
     Another
   </li>
 </ul>
 <div id="submenu-1" class="submenu-options">
  <ul>
   <li>Something-sub</li>
   <li>Something-sub</li>
   <li>Something-sub</li>
  </ul>
 </div>
 <div id="submenu-2" class="submenu-options">
  <ul>
   <li>Another-sub</li>
   <li>Another-sub</li>
   <li>Another-sub</li>
  </ul>
 </div>
</div>

更具体地说,如果我将鼠标悬停在某物上,我希望显示某物子菜单,如果鼠标离开,我希望它再次隐藏...... 我知道这个问题已经被问了很多,并且有很多方法可以做到这一点,但谷歌提出了太多令人不满意的答案。 我希望您能抽出 10 分钟的时间来帮助我摆脱痛苦,

并向您致以问候,谢谢!

Hi I'm new to JavaScript and I want to code a very simple expanding submenu.

<div id="submenu">
  <ul>
   <li>
     Something
   </li>
   <li>
     Another
   </li>
 </ul>
 <div id="submenu-1" class="submenu-options">
  <ul>
   <li>Something-sub</li>
   <li>Something-sub</li>
   <li>Something-sub</li>
  </ul>
 </div>
 <div id="submenu-2" class="submenu-options">
  <ul>
   <li>Another-sub</li>
   <li>Another-sub</li>
   <li>Another-sub</li>
  </ul>
 </div>
</div>

To be more specific, if I hover over something I want the something submenu displayed if the mouse leaves I want it to be hidden again...
I know it has been asked a lot and there are many ways to do this but Google brought up too many unsatisfying answers.
I hope you can spare 10 minutes to help me out of my misery

Regards and thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

余罪 2024-12-07 07:00:24
<div id="submenu">
  <ul>
   <li id="1">
     Something
   </li>
   <li id="2">
     Another
   </li>
 </ul>
 <div id="submenu-1" class="submenu-options">
  <ul>
   <li>Something-sub</li>
   <li>Something-sub</li>
   <li>Something-sub</li>
  </ul>
 </div>
 <div id="submenu-2" class="submenu-options">
  <ul>
   <li>Another-sub</li>
   <li>Another-sub</li>
   <li>Another-sub</li>
  </ul>
 </div>
</div>

如果您没有使用任何库,那么您需要像这样绑定它:

 var menuText;

  window.onload = function()
  {

     menuText= document.getElementById("1");
     menuText.onfocus = menuFocusHandler;
     menuText.onblur = menuBlurHandler;
  }

function menuFocusHandler()
{
document.getElementById("submenu-1").style.display="inline";
}

function menuBlurHandler()
{
document.getElementById("submenu-1").style.display="none";
}

或者您可以使用某些具有 API 的 javascript 库轻松轻松地完成此操作。其中一些库是 Jquery(最流行的库) 、Sencha 等。

其中一些库具有具有菜单实现等的扩展。

<div id="submenu">
  <ul>
   <li id="1">
     Something
   </li>
   <li id="2">
     Another
   </li>
 </ul>
 <div id="submenu-1" class="submenu-options">
  <ul>
   <li>Something-sub</li>
   <li>Something-sub</li>
   <li>Something-sub</li>
  </ul>
 </div>
 <div id="submenu-2" class="submenu-options">
  <ul>
   <li>Another-sub</li>
   <li>Another-sub</li>
   <li>Another-sub</li>
  </ul>
 </div>
</div>

if you are not using any library then you need to bind it something like this:

 var menuText;

  window.onload = function()
  {

     menuText= document.getElementById("1");
     menuText.onfocus = menuFocusHandler;
     menuText.onblur = menuBlurHandler;
  }

function menuFocusHandler()
{
document.getElementById("submenu-1").style.display="inline";
}

function menuBlurHandler()
{
document.getElementById("submenu-1").style.display="none";
}

or you can do this nicely and easily using certain javascript libraries that have APIs to do this easily.. some of these libraries are Jquery (the most popular one), Sencha etc.

Some of these libraries have extensions that have menu implementation etc.

简单气质女生网名 2024-12-07 07:00:24

我已经使用 Dojo 来完成类似的事情并取得了巨大的成功。也许是 菜单MenuBar 对您有用。

I have used Dojo for things like this with great success. Perhaps the Menu or MenuBar will be of use to you.

メ斷腸人バ 2024-12-07 07:00:24

您可以使用 jQuery 轻松完成此类操作,以这个下拉示例为例:

HTML:

<ul>
   <li>Nav Item 1</li>
   <li class="dropdown">
       Nav Item 2
       <ul style="display:none">
           <li>Sub Menu Item 1</li> 
           <li>Sub Menu Item 2</li>
           <li>Sub Menu Item 3</li>
       </ul> 
   </li>
   <li>Nav Item 3</li>
</ul>

jQuery:

$('.dropdown').hover(function() {
   $('ul', $(this)).show();
}, function() {
   $('ul', $(this)).hide();
});

You can do this sort of thing easily with jQuery, take this drop down example:

HTML:

<ul>
   <li>Nav Item 1</li>
   <li class="dropdown">
       Nav Item 2
       <ul style="display:none">
           <li>Sub Menu Item 1</li> 
           <li>Sub Menu Item 2</li>
           <li>Sub Menu Item 3</li>
       </ul> 
   </li>
   <li>Nav Item 3</li>
</ul>

jQuery:

$('.dropdown').hover(function() {
   $('ul', $(this)).show();
}, function() {
   $('ul', $(this)).hide();
});
缘字诀 2024-12-07 07:00:24

你不需要 java,因为只需简单的 CSS 即可:

li#submenu:hover div {
    display: block;
}

You do not need java for this just plain CSS will do:

li#submenu:hover div {
    display: block;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文