我好像搞不懂这个,觉得我错过了什么愚蠢的事情...
基本上,当悬停在 * 删除链接 * 上时,我会尝试对该行中的所有文本执行 * 换行 * 操作,除了其中包含<a class="remove">
的<td>
。
基本的html结构是:
<tr>
<td>Lorem ipsum text here</td>
<td>01/01/2012</td>
<!-- all <td>'s except for the Remove one should get a line-through -->
<td><a class="remove">Remove</a></td>
</tr>
查询:
$('tr').on({
'mouseover' : function () {
$(this).closest('tr').find('td').filter(function () {
var $childElems = $(this).children();
// I can see the <a class="remove"> in .children()
// But for some reason can't just test (hey there's an <a>,
// then don't apply this)
if ($childElems.find('a').length <= 0) {
return $(this).css('text-decoration', 'line-through');
}
});
},
'mouseout' : function () {
$(this).closest('tr').find('td')
.css('text-decoration', 'none');
}
}, 'a.remove');
4条答案
按热度按时间u3r8eeie1#
在
filter()
中,this
依次是td
中的每个元素,当你调用children()
时,你会得到一个jQuery对象 ,也就是<a>
,然后,你会在<a>
中搜索 * 另一个 *<a>
(这就是为什么你看不到它)。而应:
......但这并不是
filter
的真正设计目的。您应该使用filter
来减少元素集,然后对结果进行操作:baubqpgj2#
如果您不直接操作CSS属性,而是使用类来操作,这将容易得多。
将该类添加到悬停时的
tr
元素中,并使用后代选择器格式化td
:ecfdbz9o3#
qfe3c7zg4#