【问题标题】:not able to add list item between a list using jquery无法使用 jquery 在列表之间添加列表项
【发布时间】:2010-03-25 23:50:10
【问题描述】:

我有一个具有结构的有序列表

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

</ol>

我想使用 jquery 在子列表 2 和 3 之间添加一个列表项

我使用了代码:-

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

但这附加在 li “另一个测试”中,而不是在 2 li 之后

这里是example page

点击“子评论”在 li 2 中添加一个 li
点击“comment”显示子lis的结构

请帮忙

谢谢

普拉迪特

【问题讨论】:

    标签: jquery list append nested-lists


    【解决方案1】:

    使用after 代替append

    【讨论】:

    • 感谢闪电般的快速回答...:-) 顺便说一句,我在哪里可以找到 jquery 函数列表...。谢谢
    【解决方案2】:

    我对 jquery 不感兴趣...但是使用普通的 javascript 函数(以及)你可以做到:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多