这是我的 td
的 HTML:
<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();
寻求帮助和建议。谢谢
最佳答案
问题是因为 closest()
会沿着 DOM 树向上查找,而您需要沿着树向下查找子元素,因此应该使用 find()
反而。另外 end()
调用也是多余的。试试这个:
var cloneTr = $('#StudentGrid tr:last').clone();
cloneTr.find('td').contents().filter(function() {
return this.nodeType === 3;
}).remove();
关于jquery - 如何从 td 中仅删除文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34899001/