当点击带有“removerow”类的锚点时,我需要隐藏一个表格行。
<tr><td>Product Name</td><td><a class="removerow">Remove</a></td></tr>
我一直在尝试这个,但它不起作用:
$("a.removerow").click(function(){
$(tr > this).hide();
});
如何选择子表为“.removerow”的整个表格行。
我做错了什么?
感谢。
答案 0 :(得分:4)
jQuery的closest(selector)
函数将向上遍历并返回最近提供的选择器。
(如果单击的元素与给定的选择器相同,则返回该元素。)
http://api.jquery.com/closest/
$("a.removerow").click(function(e){
$(this).closest('tr').hide();
e.preventDefault();
});
e.preventDefault()
将禁用a
元素的默认行为。
答案 1 :(得分:3)
jQuery没有父选择器,但确实有parent function。
此外,tr不是链接的直接父级。相反,它是两个级别(td
是第一个父级)
$("a.removerow").click(function(){
// Called twice (first is td, second is tr)
$(this).parent().parent().hide();
});
如果层次结构中没有其他tr
,您也可以使用
$("a.removerow").click(function(){
// Called twice (first is td, second is tr)
$(this).parents('tr').hide();
});
如果tr上有一个类,你可以这样做:
$("a.removerow").click(function(){
// Called twice (first is td, second is tr)
$(this).parents('.ClassNameHere').hide();
});