当前位置: 代码迷 >> Web前端 >> 搜索下级dom对象并隐藏,点击隐藏父元素
  详细解决方案

搜索下级dom对象并隐藏,点击隐藏父元素

热度:386   发布时间:2012-10-07 17:28:51.0
搜索上级dom对象并隐藏,点击隐藏父元素
类似页面如下:

<tr>
  <td><input type="checkbox" class="chk" /></td>
  <td><div class="disabled">text to hide 1</div></td>
  <td><div class="disabled">text to hide 2</div></td>
</tr>



$("input.chk").click(function(){
  $(this).parent().parent().(".disabled").show();
}) ;


实际使用.closest() 和 .find()更合适
$("input.chk").click(function(){
  $(this).closest('tr').find(".disabled").show();
});

当然也可以

$(this).parent().parent().find(".disabled").show();


如果,有多行的话用.delegate()如下:

$("table").delegate("input.chk", "click", function(){
  $(this).closest('tr').find(".disabled").show();
});



#.delegate() instead binds one handler to the table for all of the input.chk elements to bubble up to. If you're looking to enable/disable, use hcnage and .toggle() in addition to the above, like this:
$("table").delegate("input.chk", "change", function(){
  $(this).closest('tr').find(".disabled").toggle(this.checked);
});
  相关解决方案