2016-06-14 68 views

回答

1

然後僅通過調用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>

+0

它沒有爲我工作,它刪除inpute。 –

+0

@MohsenZahedi請立即檢查 – gurvinder372

0

香草溶液

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>

相關問題