我有一个嵌套在其中的图像的TD,我想添加一个ID到它的父级,然后移除TD。以下代码将根据嵌套在其中的图像移除TD,但在移除TD之前,我想向其父表添加一个ID。有什么建议么?jQuery添加ID到父表中,然后删除TD
$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().remove();
我有一个嵌套在其中的图像的TD,我想添加一个ID到它的父级,然后移除TD。以下代码将根据嵌套在其中的图像移除TD,但在移除TD之前,我想向其父表添加一个ID。有什么建议么?jQuery添加ID到父表中,然后删除TD
$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().remove();
$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().closest("table").attr('id','newId');
$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().remove();
使用closest
和end
。 closest
找到与选择器匹配的最近的祖先元素,并且end
返回到先前的选择。
所以......
$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]')
.parent() // go to the td
.closest('table') // go to the table
.prop('id', 'foobar') // set the table's id property
.end() // go back to the td
.remove(); // remove it
良好的使用`.end()`+1 – 2011-12-15 22:21:14
添加ID,然后删除...有什么问题吗? – 2011-12-15 22:19:01
想要将ID添加到父表中。 – henryaaron 2011-12-15 22:19:31