0

我被这个难住了,也许你能帮帮我。

这是我的html:

<ul class="wpsc_product_list_categories wpsc_product_list_categories_js">
<li><a href="#" id="products_all">All products</a></li>
<li><a href="#" class="wpsc_category_link "><span class="product_category_15">cat 1</span></a>
</li>
<li><a href="#" class="wpsc_category_link "><span class="product_category_12">cat 2</span></a></li>
</ul>

这是我的js:

jQuery(".wpsc_product_list_categories_js a").click( function() {    
    jQuery('.wpsc_product_list_categories a').removeClass('wpsc-current-cat');  
    jQuery(this).addClass('wpsc-current-cat');

    var className = jQuery(this).children().attr('class');

    jQuery('.products_list_product').fadeOut(300, function() { 
        jQuery('.products_list_product').removeClass('product_item_last');
        i = 2
        jQuery('.' + className).each(function(j){
            i = i + 1;
            if (i == 3) {
                jQuery(this).addClass('product_item_last'); 
                i = 0;
            }
        });
    });
    alert(className);
    jQuery('.' + className).fadeIn(300);
    return false;
});

这个想法是当您单击其中一个链接时,跨度的类存储在变量“className”中。当您单击它一次时,它工作正常。当您再次单击它时,变量突然变为“the_original_class_name”和“product_item_last”。这不应该发生,但我不知道为什么。

有人知道吗?

4

1 回答 1

2

您在this函数内部使用,但您试图this在另一个范围内引用。

jQuery(".wpsc_product_list_categories_js a").click( function() {    
    jQuery('.wpsc_product_list_categories a').removeClass('wpsc-current-cat');  
    jQuery(this).addClass('wpsc-current-cat');

    var className = jQuery(this).children().attr('class');

    jQuery('.products_list_product').fadeOut(300, function() { 
        jQuery('.products_list_product').removeClass('product_item_last');
        var $this = $(this);      //this is the "this" that you want(i think)!!!!
        i = 2
        jQuery('.' + className).each(function(j){
            i = i + 1;
            if (i == 3) {
                $this.addClass('product_item_last');  //and use it here!!!!!
                i = 0;
            }
        });
    });
    alert(className);
    jQuery('.' + className).fadeIn(300);
    return false;
});​

在此处输入图像描述

于 2012-08-23T23:23:45.090 回答