我對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'));
};
此代碼工作正常到的HIGHlite元素邊框,但是當我恰克代碼指向這樣一個特定的元素,使用全局變量代表所選元素失敗。 我不理解「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'));
};
任何幫助將不勝感激。
'this'不是一個jQuery變量,它是一個javascript關鍵字referrencing,在這種情況下,該方法被調用的元素。只是用一些隨機變量取代它,即使正確設置也不會工作,因爲沒有自動引用您的類或其他任何東西。 – adeneo
你會在.eq之後使用圓括號,而不是方括號嗎? –
如何使用'blockHighlite'? –