2016-01-20 62 views
4

这是我td的HTML:如何从td中只删除文本?

<td class="SmallCols PadOn"> 
    6 
    <input type="hidden" id="HiddenID" value="0" name="HiddenID"> 
</td> 

6里面的td唯一的文本。 td也有隐藏的字段,我不想删除。我只是想删除6的文字。我试过这个代码,但没有运气:

var cloneTr = $('#StudentGrid tr:last').clone(); 
cloneTr.closest('td').contents().filter(function() { 
    return this.nodeType === 3; 
}).remove().end().end(); 

寻求帮助和建议。谢谢

回答

1

问题是因为closest()上去的DOM树,而你需要去树下找到子元素,所以应该使用find()来代替。 end()呼叫也是多余的。试试这个:

var cloneTr = $('#StudentGrid tr:last').clone(); 
cloneTr.find('td').contents().filter(function() { 
    return this.nodeType === 3; 
}).remove(); 

Example fiddle

+0

工作.........感谢 – Thomas

+0

没问题,很高兴提供帮助。 –