jQuery .appendTo(element) 不适用于所有 IE 版本?
我有以下 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);
});
}
有人对这个问题有解决方法的建议吗?
I have the following Javascript code that sorts divs on my page:
$(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>
This sorting function works well in all browsers but not at all in IE (all versions of IE). When I run the script by changing the select list option (calls the sort function), everything disappears in IE. Looks like it may be an issue with the jQuery .appentTo(element) not working in IE or something in the reorderEL function:
function reorderEl(el){
var allProductsContainer = $('#product-collection');
allProductsContainer.html('');
el.each(function(){
$(this).appendTo(allProductsContainer);
});
}
Does anyone have suggestions for a workaround on this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我突然想到的事情:
可以重写为:
另外,您可能应该重做淡入淡出动画:
这样,它会淡出,然后重新排序内容,然后淡入。fadeOut() 将立即返回,它不会'不等待动画完成,因此必须提供回调函数。
The thing that jumps out at me:
could be rewritten as:
Also, you should probably redo the fade animation:
This way, it fades out, then reorders the contents, then fades back in. fadeOut() will return immediately, it won't wait for the animation to finish, so a callback function has to be provided.