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;
});

リンクの 1 つをクリックすると、スパンのクラスが変数「className」に格納されます。1回クリックすると正常に動作します。もう一度クリックすると、変数が突然「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 に答える