通过jquery添加html标记

通过jquery添加html标记,jquery,insert,Jquery,Insert,我有下面的html <li class="cat-item cat-item-3 collapsable"> <div class="someclass"></div> <a href="http://demo.mysite.com/category/parent-category-i/" title="View all posts filed under Parent Category I"> Parent Category I <

我有下面的html

<li class="cat-item cat-item-3 collapsable">
<div class="someclass"></div>
<a href="http://demo.mysite.com/category/parent-category-i/" title="View all posts filed under Parent Category I">
    Parent Category I
</a>
<ul class="children">
    <li class="cat-item cat-item-6">
        <a class="" href="http://demo.mysite.com/category/parent-category-i/child-category-i/" title="View all posts filed under Child Category I">
            Child Category I
        </a>
    </li>
    <li class="parent cat-item cat-item-7 collapsable lastCollapsable">
        <div class="someclass"></div>
        <a href="http://demo.mysite.com/category/parent-category-i/child-category-ii/" title="View all posts filed under Child Category II">
            Child Category II
        </a>
        <ul class="children">
            <li class="cat-item cat-item-9 last">
                <a href="http://demo.mysite.com/category/parent-category-i/child-category-ii/grandchild-category-i/" title="View all posts filed under Grandchild Category I">
                    Grandchild Category I
                </a> 
            </li>
  • 使用上面的html和jquery,如何添加

    <span class="folder">
    
    
    
    在每个li.parent类和每个parents锚定标记之后。 最后,上面的html看起来像

    <li class="cat-item cat-item-3 collapsable">
    <div class="someclass"></div>
    <a href="http://demo.mysite.com/category/parent-category-i/" title="View all posts filed under Parent Category I">
        Parent Category I
    </a>
    <ul class="children">
        <li class="cat-item cat-item-6">
            <a class="" href="http://demo.mysite.com/category/parent-category-i/child-category-i/" title="View all posts filed under Child Category I">
                Child Category I
            </a>
        </li>
        <li class="parent cat-item cat-item-7 collapsable lastCollapsable">
            <span class="folder"> // <-------------------- Want this
                <div class="someclass"></div>
                <a href="http://demo.mysite.com/category/parent-category-i/child-category-ii/" title="View all posts filed under Child Category II">
                    Child Category II
                </a>
            </span> // <------------------------ Closing span of what I Want
            <ul class="children">
                <li class="cat-item cat-item-9 last">
                    <a href="http://demo.mysite.com/category/parent-category-i/child-category-ii/grandchild-category-i/" title="View all posts filed under Grandchild Category I">
                        Grandchild Category I
                    </a> 
                </li>
    

    • //这应该可以。包装器函数无法工作,因为您试图同时包装多个元素

      var x  = $('.parent.cat-item-7').contents();
      $('.parent.cat-item-7').empty();
      $('.parent.cat-item-7').append('<span class="folder"></span>');
      
      $('.parent.cat-item7 .folder').append(x);
      
      var x=$('.parent.cat-item-7').contents();
      $('.parent.cat-item-7').empty();
      $('.parent.cat-item-7')。追加('');
      $('.parent.cat-item7.folder')。追加(x);
      
      这应该可以。包装器函数无法工作,因为您试图同时包装多个元素

      var x  = $('.parent.cat-item-7').contents();
      $('.parent.cat-item-7').empty();
      $('.parent.cat-item-7').append('<span class="folder"></span>');
      
      $('.parent.cat-item7 .folder').append(x);
      
      var x=$('.parent.cat-item-7').contents();
      $('.parent.cat-item-7').empty();
      $('.parent.cat-item-7')。追加('');
      $('.parent.cat-item7.folder')。追加(x);
      
      可用于包装元素的子元素:

      $(".parent").children().wrapAll("<span class='folder' />");
      
      以下是。

      您可以用来包装元素的子元素:

      $(".parent").children().wrapAll("<span class='folder' />");
      

      以下是。

      请尝试以下代码:

      $("<span class='folder' />").appendTo('.parent');
      
      $(“”).appendTo('.parent');
      

      这将使它发生。

      请尝试以下代码:

      $("<span class='folder' />").appendTo('.parent');
      
      $(“”).appendTo('.parent');
      

      这会让它发生。

      这将包装每个孩子,而不是所有的孩子,它将复制文本:@jamesalardice是的,我注意到这就是我更改它的原因:)但仍然感谢警告,如果我得到了我无法正确解释的意外结果,请检查这里[链接]这将包装每个孩子,而不是所有的孩子,它将复制文本:@jamesalardice是的,我注意到这就是我更改它的原因:)但仍然感谢警告,如果我得到了我无法正确解释的意外结果,请检查这里[链接]这将只在
      .parent
      的所有子级之后插入一个空的
      span
      。OP希望包装
      .parent
      的子项。这将在
      .parent
      的所有子项之后插入一个空的
      span
      。OP希望包装
      .parent
      的子项。感谢您的支持,但仍然无法获得所需的结果。我的最终代码和结果显示在下面提到的演示链接中。如果您能多挖掘一点,我将不胜感激。谢谢您的支持,但仍然无法获得所需的结果。我的最终代码和结果显示在下面提到的演示链接中。如果你能再挖一点,我会很感激的