0

jquery変数「this」の値について簡単に質問します。

以下に示すサンプルコードをいくつか取りました...

function blockHighlite()
{
//  alert ('block ' + $gCurrentClass + ' index ' + $gIndex);

        $(this).data('bgcolor', $(this).css('border-color'));
        $(this).css('border-color','rgba(255,128,0,.5)');
        $(this).css('border-color', $(this).data('bgcolor'));
  };

このコードは要素の境界線を強調表示するために正常に機能しますが、このような特定の要素を指すようにコードを変更すると、グローバル変数を使用して選択した要素を表すと失敗します。「this」変数の使用法を理解していませんか?変数$gCurrentClassと$gIndexは、選択した要素のクラスとインデックスにすぎません。

function blockHighlite()
{
    alert ('block ' + $gCurrentClass + ' index ' + $gIndex);
        $gCurrentClass.eq[$gIndex].data('bgcolor', $gCurrentClass.eq[$gIndex].css('border-color'));
        $gCurrentClass.eq[$gIndex].css('border-color','rgba(255,128,0,.5)');
        $gCurrentclass.eq[$gIndex].css('border-color', $gCurrentClass.eq[$gIndex].data('bgcolor'));
  };

どんな助けでもありがたいです。

4

1 に答える 1

1

クラス名を表す文字列が含まれていると仮定すると、それをクエリセレクタとして$gCurrentClassjQueryコンストラクタ()に渡す必要があります。$次のことを試してください。

function blockHighlite()
{
    alert ('block ' + $gCurrentClass + ' index ' + $gIndex);
    $('.'+$gCurrentClass).eq($gIndex).data('bgcolor', $('.'+$gCurrentClass).eq($gIndex).css('border-color'));
    $('.'+$gCurrentClass).eq($gIndex).css('border-color','rgba(255,128,0,.5)');
    $('.'+$gCurrentclass).eq($gIndex).css('border-color', $('.'+$gCurrentClass).eq($gIndex).data('bgcolor'));
};
于 2012-12-08T17:42:33.407 に答える