jQuery 环绕第一深度 li's
我正在尝试让一些 jQuery 代码适用于菜单.. 我需要将
的
包裹在我的第一深度
“我的 HTML 语法”周围,看起来像这样。
<ul>
<li id="" class="menu-item">
<a class="" href="#">test1</a>
<div class="sub">
<ul class="sub-menu">
<li id="" class="menu-item">
<a class="" href="#">test2</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
<li id="" class="menu-item">
<a class="" href="#">test3</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
插入后应该是这样的
标签 <ul>
<li id="" class="menu-item">
<a class="" href="#">test1</a>
<div class="sub">
<ul class="sub-menu">
<ul class="drop_menu">
<li id="" class="menu-item">
<a class="" href="#">test2</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
</ul>
<ul class="drop_menu">
<li id="" class="menu-item">
<a class="" href="#">test3</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
</ul>
</ul>
</div>
</li>
</ul>
干杯!
Im trying to get some jQuery code working for a menu..
I need to wrap a <ul>
around my first-depth <li>
's
My HTML Syntax look like that..
<ul>
<li id="" class="menu-item">
<a class="" href="#">test1</a>
<div class="sub">
<ul class="sub-menu">
<li id="" class="menu-item">
<a class="" href="#">test2</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
<li id="" class="menu-item">
<a class="" href="#">test3</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
This is how it should look after inserting the <ul>
tags
<ul>
<li id="" class="menu-item">
<a class="" href="#">test1</a>
<div class="sub">
<ul class="sub-menu">
<ul class="drop_menu">
<li id="" class="menu-item">
<a class="" href="#">test2</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
</ul>
<ul class="drop_menu">
<li id="" class="menu-item">
<a class="" href="#">test3</a>
<ul class="sub-menu">
<li id="" class="menu-item"><a class="" href="">test</a></li>
<li id="" class="menu-item"><a class="" href="">test</a></li>
</ul>
</li>
</ul>
</ul>
</div>
</li>
</ul>
The <ul class="drop_menu">
should only be wrapped around the first-level li's... Didn't find any solution for that until now.. Hope anyone has an idea..
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要直接选择
的
内的所有
li
:
a > b
子选择器 将选择作为a
直接子级的所有b
元素。编辑:您还在寻找
.unwrap()
方法。You want to select all
li
s directly inside the<div>
's<ul>
:The
a > b
child selector will select allb
elements that are a direct child of ana
.EDIT: You're also looking for the
.unwrap()
method.如果环绕第一级 li,则:
$('ul.drop_menu > li').each(function(){
//使用这个在每个li中做一些事情
});
if wrapping around 1st level li's then:
$('ul.drop_menu > li').each(function(){
//do something in each li using this
});
下面的 jQuery 应该可以解决问题......
The following jQuery should do the trick...