【发布时间】:2012-03-11 18:36:54
【问题描述】:
以下代码对所有带有class="hole" 的元素执行.css({"background":"black"});,但是我试图让它在带有class="hole" 和data-hole-key="[hole_key_variable]" 的元素上执行它。
缺少什么?
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>
顺便说一句,为什么这个(错误的)代码在没有双换行的情况下根本不起作用:
var holeKey = $($(this).data('holeKey'));
【问题讨论】:
-
您是否尝试过组合选择器,例如
$('.hole[data-hole-key]');?