开发者

not able to add list item between a list using jquery

开发者 https://www.devze.com 2022-12-24 12:49 出处:网络
I m having a ordered list having the structure <ol> <li> </li> <li> </li> <li>

I m having a ordered list having the structure

<ol>
      <li>
      </li>
      <li>
      </li>
      <li>
         <ol>
              <li>
                 Test
              </li>
              <li>
                 another test
              </li>
              <li>
                   <a href='#'>Add </a>
              </li>
         </ol>
      </li>

</ol>

I want to add a list item between sublist 2 and 3 using jquery

I used the code: -

$("ol#update li ol li:eq(1)").append("<li> test </li>");

But this appends inside the li "another test" and not after the 2 li

Here is the example开发者_如何学编程 page

On-click "sub-comment" adds a li inside li 2

Clicking on "comment" shows the structure of the sub lis

Please help

Thanks

Pradyut


Use after instead of append


I'm not big into jquery... but using normal javascript functions (as well) you can do:

var item = $("ol#update li ol li:eq(1)");
var thingToAppend = document.createElement('li');
thingToAppend.innerHTML = ' test ';
item.parentNode.insertBefore(item, thingToAppend);
// or
item.parentNode.appendChild(thingToAppend);
// or apparently.... with jQuery something like:
item.after(thingToAppend);
0

精彩评论

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

关注公众号