当单击类“removerow”的锚点时,我有一个需要隐藏的表行。jquery中的父类选择器
<tr><td>Product Name</td><td><a class="removerow">Remove</a></td></tr>
我一直想这一点,但它不工作:
$("a.removerow").click(function(){
$(tr > this).hide();
});
我怎样才能选择整个表格行与“.removerow”的孩子。
我在做什么错?
谢谢。
当单击类“removerow”的锚点时,我有一个需要隐藏的表行。jquery中的父类选择器
<tr><td>Product Name</td><td><a class="removerow">Remove</a></td></tr>
我一直想这一点,但它不工作:
$("a.removerow").click(function(){
$(tr > this).hide();
});
我怎样才能选择整个表格行与“.removerow”的孩子。
我在做什么错?
谢谢。
jQuery的closest(selector)
函数将向上遍历并返回提供的最近的选择器。
(如果点击的元素是相同的给定的选择,那么它返回。)
http://api.jquery.com/closest/
$("a.removerow").click(function(e){
$(this).closest('tr').hide();
e.preventDefault();
});
的e.preventDefault()
将禁用a
元素的默认行为。
jQuery没有父选择器,但它的确有parent function。
另外,tr不是链接的直接父节点。相反,它是向上两级(td
是第一父)
$("a.removerow").click(function(){
// Called twice (first is td, second is tr)
$(this).parent().parent().hide();
});
如果没有其他tr
S IN的层次,你也可以使用
$("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();
});