开发者

how to insert a new li tag at the specified location?

开发者 https://www.devze.com 2022-12-25 11:57 出处:网络
I wanted to insert a li tag in the middle of a list of li tags based on a css class set to the li tag using jQuery. Consider the following

I wanted to insert a li tag in the middle of a list of li tags based on a css class set to the li tag using jQuery. Consider the following

<ul class="myList">
     <li><a href="#">test 1</a></li>
     <li class="active"><a href="#">test 2</a></li>
     <li><a href="#">test 3</a&g开发者_StackOverflow社区t;</li>
     <li><a href="#">test 4</a></li>
    <li><a href="#">test 5</a></li>
    <li><a href="#">test 6</a></li>
</ul>

I wanted to insert a new li tag after the li tag set to active. So the output will be like this.

<ul class="myList">
     <li><a href="#">test 1</a></li>
     <li class="active"><a href="#">test 2</a></li>
     <li><a href="#">My new Tag</a></li>
     <li><a href="#">test 3</a></li>
     <li><a href="#">test 4</a></li>
    <li><a href="#">test 5</a></li>
    <li><a href="#">test 6</a></li>
</ul>

I tried with .appendTo, .insertAfter, .append etc. but could not get the result I wanted. Any idea how this can be achieved?


$('li.active').after('<li><a href="#">My new Tag</a></li>');


Try this:

$('<li><a href="#">content here</a></li>').insertAfter('ul.myList li.active');
0

精彩评论

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