【发布时间】: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