2011-12-15 34 views
0

我有一个嵌套在其中的图像的TD,我想添加一个ID到它的父级,然后移除TD。以下代码将根据嵌套在其中的图像移除TD,但在移除TD之前,我想向其父表添加一个ID。有什么建议么?jQuery添加ID到父表中,然后删除TD

$('td img[src="/v/vspfiles/templates/cyberfront/images/RBox_Border_Left_Top.gif"]').parent().remove(); 
+0

添加ID,然后删除...有什么问题吗? – 2011-12-15 22:19:01

+0

想要将ID添加到父表中。 – henryaaron 2011-12-15 22:19:31

回答

2
$('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(); 
4

使用closestendclosest找到与选择器匹配的最近的祖先元素,并且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 
+0

良好的使用`.end()`+1 – 2011-12-15 22:21:14

相关问题