开发者

add li to middle of the ul (jquery)

开发者 https://www.devze.com 2022-12-20 10:44 出处:网络
How one can add li to middle of the ul. 开发者_Go百科 For example if i have list like this: <ul>

How one can add li to middle of the ul.

开发者_Go百科

For example if i have list like this:

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

And i want to add addiion li (<li>new</li>) after 3rd list?


var middle = Math.ceil($("#myUL li").length / 2);
$("#myUL li:nth-child(" + middle + ")").after('<li>Item New</li>');


Try this out:

$('ul li:nth-child(3)').after('<li>new</li>');


Try this:

$('ul li:eq(2)').after(`<li>new</li>`)

Use ' or " within braces I have used ` since it was getting formatted as html.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号