简体   繁体   English

在detach和appendTo元素复制之后

[英]After detach and appendTo elements are duplicated

When I detach and then re-attach elements using detach and appendTo the elements are duplicated. 当我分离然后再使用detach和appendTo附加元素时,这些元素将重复。

Edit : Added the ejs view that accompanies the javascript. 编辑 :添加了JavaScript随附的ejs视图。 Also wrapped the groceryItemsContainer with jQuery. 还用jQuery包装了杂货项容器。

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

Here is the ejs: 这是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>

Here is the javascript: 这是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>

You may want to check the jQuery documentation on detach . 您可能需要查看detachjQuery文档

Specifically, this part of the example: 具体来说,这部分示例:

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

EDIT: From the viewpoint of the code you provided, it seems that it should work. 编辑:从您提供的代码的角度来看,它似乎应该工作。 Please add more code so that we can diagnose your problem. 请添加更多代码,以便我们诊断您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM