我想删除自定义td的值。如何在没有删除输入的情况下清空td的值?
<td> <input type="hidden" name="myinput" value="1"> some text </td>
我不想删除输入要删除一些文本only.any帮助吗?
我想删除自定义td的值。如何在没有删除输入的情况下清空td的值?
<td> <input type="hidden" name="myinput" value="1"> some text </td>
我不想删除输入要删除一些文本only.any帮助吗?
然后仅通过调用text
方法
例如更新textContent
,
//$("td").text(""); //Doesn't work
//$("td")[0].textContent = ""; //Doesn't work
//convert childNodes to Array and then iterate through the same
Array.prototype.slice.call($("td")[0].childNodes).forEach(function(node){
node.nodeType == 3 && node.parentNode.removeChild(node); //if the node is a text node then remove the same
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>
<input type="hidden" name="myinput" value="1">some text</td>
</tr>
</table>
它没有为我工作,它删除inpute。 –
@MohsenZahedi请立即检查 – gurvinder372
香草溶液
var td = document.querySelector('td'),
child = td.childNodes
for (var i = 0;i < child.length;i++){
if (child[i].nodeType === 3){
td.removeChild(child[i])
}
}
<table>
<td> <input type="text" name="myinput" value="1"> some text </td>
</table>
http://stackoverflow.com/questions/3421999/jquery-remove-only-text-content-from-a-div – szilagyif