显示/隐藏非父元素和子元素时,悬停将不起作用
我有这样的代码
<ul id="mainmenu">
<li><a href="#">mainlink1</a></li>
<li><a href="#">mainlink2</a></li>
<li><a href="#">mainlink3</a></li>
<ul>
,但我希望“mainlink2”有一个子菜单,我不想像子菜单一样放置它,因为当我将子菜单设置为“绝对”时,我无法将其宽度适合屏幕,并且中心,所以我在“ul”之外创建一个 div,如下所示...
<div>
<ul id="submenu">
<li>sub1</li>
<li>sub2</li>
<li>sub3</li>
</ul>
</div>
当我调用 javascript .hover 时,我看不到子菜单。我尝试通过记住主菜单的索引来将类添加到“子菜单”,当它匹配时,我添加类以显示子菜单,当不悬停时,我删除类。但是当我将鼠标从主菜单中离开时,悬停功能无法执行此操作,子菜单将隐藏。
I have a code like this
<ul id="mainmenu">
<li><a href="#">mainlink1</a></li>
<li><a href="#">mainlink2</a></li>
<li><a href="#">mainlink3</a></li>
<ul>
but i want the 'mainlink2' has a submenu, I don't want to place it like child because when i set the submenu to be 'absolute' i can't fit its width to the screen and be the center so i create a div outside the 'ul' like this ...
<div>
<ul id="submenu">
<li>sub1</li>
<li>sub2</li>
<li>sub3</li>
</ul>
</div>
when i call javascript .hover i can't see the submenu. i try to add a class to "submenu" by remember the index of mainmenu and when it match i add class to show the submenu, when not hover i remove class. But the hover function can't do this when i leave a mouse from main menu the submenu is hide.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
HTML:
JQUERY:
Try this:
HTML:
JQUERY: