分离和appendTo元素重复后

时间:2017-05-09 00:49:50

标签: javascript jquery

当我分离然后使用detach和append重新附加元素时,元素被复制。

修改:添加了javascript附带的ejs视图。还使用jQuery包装了groceryItemsContainer。

$groceryItems.appendTo( $('.groceryItemsContainer') );

这是ejs:

<div class="panel-group material_accordion" id="accordion" role="tablist" aria-multiselectable="true">
<div class="row accord" role="tab" id="headingFive">
    <h4 class="panel-title centerBlock">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseFive" aria-expanded="true"
           aria-controls="collapseFive">
            Highest Rated Grocery Items<span style="margin-left: 1em" id="chevron_toggleable_five" class="chevron_toggleable glyphicon glyphicon-chevron-right"></span>
        </a>
    </h4>
</div>
<div id="collapseFive" class="panel-collapse collapse material_accordion_collapse" role="tabpanel"
     aria-labelledby="headingFive">
    <div class="panel-body">
        <div class="container">
            <% if (giInStore.length !== 0) { %>
            <div class="alignCenter">
                <div class="btn-group">
                    <button type="button" onclick="sortByAvgRating()" class="btn btn-primary starSRating">Stars</button>
                    <button type="button" class="btn btn-primary flavor">Your Flavor</button>
                </div>
            </div>

            <% giInStore.forEach(function(eachResult, index) { %>
            <div class="groceryItemsContainer">
                <div class="row groceryItems">
                    <div class="name col-xs-4 col-sm-4 col-md-4 alignCenter">
                     <p><%= eachResult.groceryItemName %></p>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 rating-stars-container alignCenter ">
                        <% if (eachResult.avgRating !== null) { %>
                            <div class="rating-stars inline-flex-block">
                                <div name="avgStarRating" class="ratebox" data-id="mRating-<%=index %>" data-rating="<%= eachResult.avgRating %>"></div>
                                <span class="green">(<%= eachResult.count ? eachResult.count : '0' %>) </span>
                            </div>
                        <% } %>
                    </div>
                    <div class="col-xs-4 col-sm-4 col-md-4 alignCenter">
                        <% if (eachResult.minRatedPrice || eachResult.maxRatedPrice)  { %>
                            <% if(eachResult.minRatedPrice === eachResult.maxRatedPrice) { %>
                            <p>$ <%= eachResult.minRatedPrice %> </p>
                            <% } else { %>
                            <p>$ <%= eachResult.minRatedPrice %> - $ <%= eachResult.maxRatedPrice %></p>
                            <% } %>
                        <% } else { %>
                            <p> - </p>
                        <% } %>
                    </div>
                </div>
            </div>
            <% }) %>
            <% } else { %>
                <div class="text-center">
                    <p>"No grocery items have been rated yet." </p>
                </div>
            <% } %>
        </div>
    </div>
</div>

这是javascript:

<script>

function sortByAvgRating() {

var $groceryItems = $('.groceryItems');// grab all of the grocery items

$groceryItems.detach();// detach the elements from the DOM

$groceryItems.sort(function(a, b) {// sort the grocery items by the rating attribute
    return +a.children[1].children[0].children[0].dataset.rating - +b.children[1].children[0].children[0].dataset.rating;
});

$groceryItems.appendTo( $('.groceryItemsContainer') );// apppend the sorted array of elements to the DOM inside the groceryItemsContainer.  The problem is there are now double the number of original elements.  

</script>

1 个答案:

答案 0 :(得分:-1)

您可能需要查看jQuery documentation on detach

具体来说,这部分示例:

var p;
$( "button" ).click(function() {
  if ( p ) {
    p.appendTo( "body" );
    p = null;
  } else {
    p = $( "p" ).detach();
  }
});

编辑:从您提供的代码的角度来看,它似乎应该有效。请添加更多代码,以便我们诊断您的问题。