【问题标题】:jQuery .appendTo(element) not working in all IE versions?jQuery .appendTo(element) 不适用于所有 IE 版本?
【发布时间】:2011-07-21 23:54:24
【问题描述】:

我有以下 Javascript 代码对我的页面上的 div 进行排序:

$(function() {

    var productContainer = $('.productBoxWrapper');

    function sortByPriceAsc (a,b){
        return Number( jQuery(a).find('.productPriceForSorting').text() - $(b).find('.productPriceForSorting').text() );
    }

    function sortByPriceDesc (a,b){
       return Number( $(b).find('.productPriceForSorting').text() - $(a).find('.productPriceForSorting').text() );
    }

    function sortByTitleAsc (a,b){    
        return $(a).find('.productTitleForSorting').text() > $(b).find('.productTitleForSorting').text() ? 1 : -1;    
    }

    function sortByTitleDesc (a,b){
       return $(b).find('.productTitleForSorting').text() > $(a).find('.productTitleForSorting').text() ? 1 : -1;
    }

    function reorderEl(el){
        var allProductsContainer = $('#product-collection');
        allProductsContainer.html('');   
        el.each(function(){        
            $(this).appendTo(allProductsContainer);        
        });
    }
    function fadeContainer() {
        productContainer.fadeOut('fast');
        productContainer.fadeIn('fast');
    }    

    $('.dk').dropkick({

          change: function () {

            var selectedOptionValue = $('#sortThisCollectionBaby option:selected').val();

            if (selectedOptionValue == 'price-low-to-high')
            {
                reorderEl(productContainer.sort(sortByPriceAsc));
                fadeContainer(); 
            }
            else if (selectedOptionValue == 'price-high-to-low')
            {
                reorderEl(productContainer.sort(sortByPriceDesc));
                fadeContainer();
            }
            else if (selectedOptionValue == 'alphabetically-a-to-z')
            {
                reorderEl(productContainer.sort(sortByTitleAsc));
                fadeContainer();
            }
            else if (selectedOptionValue == 'alphabetically-z-to-a')
            {
                reorderEl(productContainer.sort(sortByTitleDesc));
                fadeContainer();
            }
            else if (selectedOptionValue == 'best-selling')
            {
                window.location.reload( true );  
                fadeContainer();
            }
          } // function    

    }); // dropkick

    });

HTML:

   <ul id="product-collection">

     <div class="productBoxWrapper">        
        <li>
    <div class="product-info">
         <h4>
            <a class="productTitleForSorting" href="my-product">MY PRODUCT</a><br>
         </h4>
         <div class="product-price">
        <span id="listPrice">Retail: $26.99</span>                                                     
            <span style="display:none;" class="productPriceForSorting">895</span>
             <span>Our Price: $8.95</span>
         </div>                   
    </div>              
    </li>
    div class="productBoxBottom">           

    <!-- view product button -->
    <a href="my-product">View</a>                       
    </div>
    </div>

    <!-- I HAVE MORE DIVS FOR OTHER PRODUCTS HERE, SAME HTML AS ABOVE -->

    </ul>

这个排序功能在所有浏览器中都能正常工作,但在 IE(所有版本的 IE)中根本不行。当我通过更改选择列表选项(调用排序函数)运行脚本时,IE 中的所有内容都消失了。看起来这可能是 jQuery .appentTo(element) 在 IE 中不起作用或 reorderEL 函数中的问题:

function reorderEl(el){
var allProductsContainer = $('#product-collection');
allProductsContainer.html('');   
el.each(function(){        
    $(this).appendTo(allProductsContainer);        
});

}

有没有人建议解决这个问题?

【问题讨论】:

  • 我得说,jQuery() 标记让人难以阅读。如果您使用$(),会更容易阅读。
  • 感谢汤姆现在改变了...忘记了...
  • @Tomm 对于我们中的一些人来说,我们有多个框架,但不能这样做。习惯了就不难读了。

标签: jquery internet-explorer appendto


【解决方案1】:

向我跳出来的东西:

function reorderEl(el){
    var allProductsContainer = $('#product-collection');
    allProductsContainer.html('');   
    el.each(function(){        
        $(this).appendTo(allProductsContainer);        
    });
}

可以改写为:

function reorderEl(el){
    $("#product-collection").empty().append(el);
}

另外,您可能应该重做淡入淡出动画:

productContainer.fadeOut("fast", function() {
                                     reorderEl(productContainer.sort(sortByPriceAsc));
                                     productContainer.fadeIn();
                                 });

这样,它会淡出,然后重新排序内容,然后淡入。fadeOut() 会立即返回,不会等待动画完成,因此必须提供回调函数。

【讨论】:

  • 丹尼斯,非常感谢!那成功了。它现在也可以在 IE 中使用。对 reorderEl 函数进行了多么大的清理! :) 褪色也是一样,现在在视觉上更有意义。
  • 我对 DOM 操作做了更多研究,您甚至可能不需要 empty() 调用。 append() 将在将元素添加到新父元素之前将其与其当前元素分离。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 2015-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-24
  • 1970-01-01
相关资源
最近更新 更多