下面的代码在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