简体   繁体   English

把李换成另一个李

[英]Wrap li into another li

I have below ul list. 我有以下ul列表。 In which I want to wrap last three li into another ul li tag. 其中我想将最后三个li包装成另一个ul li标签。

My html 我的HTML

<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>

I want 我想要

<ul>
<li>1</li>
<li>
<ul>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</li>
</ul>

my code 我的代码

$('ul li:not(:first)').wrap('<li></li>');

But it's wraping individual li into another li. 但它正在将个人李某变成另一个人。 pls help 请帮助

All you need here in this context is .wrapAll() 在这个上下文中你需要的只是.wrapAll()

Try, 尝试,

$('ul li:not(:first)').wrapAll('<li><ul></ul></li>');

DEMO DEMO

try this 试试这个

according to me we should avoid :first pseudo class because in many cases this not work so try following code 根据我,我们应该避免:第一个伪类因为在许多情况下这不起作用所以尝试下面的代码

 $(function(){

$('ul li').eq(0).addClass('hi');

$('li').not('.hi').wrapAll('<ul><li></li></ul>');

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM