使用 jQuery 获取另一个元素
我构建了多级菜单,我的 HTML 结构如下所示:
<ul>
<li>item 1</li>
<li>item 2</li>
<li>
<ul> #this is set up as display: none;
<li>subitem 1</li>
<li>subitem 2</li>
<li>subitem 3</li>
</ul>
</li>
<li>item 3</li>
<li>item 4</li>
</ul>
我正在解决一个问题,如何在将光标移动到 item1 上后显示所有子项目。 我可以做这样的事情:
$('ul li ul').mouseover(function() {
$(this).find('li').show();
});
但这对我不起作用...任何人都可以帮助我,如何显示鼠标悬停事件的子 ul 项目块?
谢谢
编辑:谢谢你们的回复,感谢你们的帮助,我已经发现了我的愚蠢错误。
I build multi level menu and my HTML structure looks like this:
<ul>
<li>item 1</li>
<li>item 2</li>
<li>
<ul> #this is set up as display: none;
<li>subitem 1</li>
<li>subitem 2</li>
<li>subitem 3</li>
</ul>
</li>
<li>item 3</li>
<li>item 4</li>
</ul>
And I am solving a question, how to display all subitems after moving the cursor on the item1.
I can do something like this:
$('ul li ul').mouseover(function() {
$(this).find('li').show();
});
But this doesn't works me... could anyone help me, please, how to display sub-ul block of items for mouseover event?
Thank you
EDIT: Thanks for your replies guys, I already found my stupid fault thanks to your helps.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其附加到父
LI
,否则不会显示供mouseover
触发的元素。另请注意,如果包含
UL
的LI
中只有UL
与不显示的LI
的,也可能很难鼠标悬停
。http://jsfiddle.net/kSq4T/1/
Attach it to the parent
LI
, otherwise there is not an element that is displayed for themouseover
to fire on.Note as well that if all you have in the
LI
containing theUL
is theUL
with the non-displayedLI
's, it will potentially be hard tomouseover
that as well.http://jsfiddle.net/kSq4T/1/
您始终可以向 item 元素添加一个类并执行如下操作:
You could always add a class to your item elements and do something like this:
在
$(document).ready
中编写您的函数write your function in
$(document).ready