我想刪除自定義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