3

私はfancyboxを使用していて、fancyboxが実行される前に要素クラスを取得したいと思っています。ので、私は持っています:

$(".agent-file-popup").fancybox({
    'onStart': function () {
        console.log($(this).attr('class'));
        console.log($(".agent-file-popup").attr('class'));
        return true;
    }
});

最初のログは「未定義」を出力しますが、2番目のログは正しいクラスを出力します。この状況で「this」を要素として使用できないのはなぜですか?

4

2 に答える 2

3

$(this)は、現在の要素がフォーカスであることを示す非常に人気のある構成の1つであり、イベントおよびセレクター関数内で使用できます。thisこれは、jQueryの関数へのアクセスを提供するためにjQueryの関数でラップされたJavaScriptの構成と同じです。

$(".user").click(function() {
    //Here $(this) will represent the element that was click with class .user
});

プラグインは通常、jQueryのjQuery()関数の拡張機能として開発されており、現在の要素を検出する責任はほとんどありません。

したがって、プラグインを初期化するとき、プラグイン$(this)は簡単に何も表さない可能性があります。

Fancyboxには、現在の要素を取得する方法があります。

onstart: function(itemArray, selectedIndex, selectedOpts){
 // selectedIndex holds the current selected box on itemArray as the collection object.
}
于 2013-03-09T22:26:10.803 に答える
0

これを試して:

$(".agent-file-popup").fancybox({
        'onStart' : function() {
           console.log($(this.element).attr('class'));
           console.log($(".agent-file-popup").attr('data-paid'));
            return true;
        }
    });
于 2013-03-09T22:20:46.233 に答える