下面的代碼在class="hole"
的所有元素上執行.css({"background":"black"});
,但是我試圖讓它在具有class="hole"
和data-hole-key="[hole_key_variable]"
的元素上執行此操作。jQuery選擇器瘋狂
什麼是缺失?
的jQuery:
// on elements with class "hole" hovered
$('.hole').hover(
function(){
// get the data value of the currently hovered element
var holeKey = $($(this).data('holeKey'));
// on all elements with that class "hole" and specified data value, change bgcolor
$('.hole').data(holeKey).css({"background":"black"});
},
function(){
var holeKey = $($(this).data('holeKey'));
$('.hole').data(holeKey).removeAttr('style');
}
);
HTML:
<td class="hole" data-hole-key="1">text</td>
<td class="hole" data-hole-key="2">text</td>
<td class="hole" data-hole-key="3">text</td>
BTW,爲什麼這(錯誤)的代碼不工作沒有雙重包裝這一行:
var holeKey = $($(this).data('holeKey'));
您是否嘗試過的組合選擇,例如'$('。hole [data-hole-key]');'? – rjz 2012-03-11 18:39:10