问题
I need to delete text from a html node, but keep the inner HTML Anyone who knows how to do this?
Example:
<td class="ms-list-addnew">
<a class="ms-herocl" id="idHPNewDocument"><span>new document</span></a>
or drag files here
</td>
Needed result (remove the " or drag files here"):
<td class="ms-list-addnew">
<a class="ms-herocl" id="idHPNewDocument"><span>new document</span></a>
</td>
回答1:
If you want to keep what you have, this can work for you. Otherwise you can add a span
tag around it with an id and remove it.
var text = $(".ms-list-addnew").html()
text = text.replace("or drag files here","");
$(".ms-list-addnew").html(text);
回答2:
There are many ways to do this. One way is to put the text in another element (say, a span
) and use .remove():
<td class="ms-list-addnew">
<a class="ms-herocl" id="idHPNewDocument"><span>new document</span></a>
<span id="dragText">or drag files here</span>
</td>
Then:
jQuery("#dragText").remove();
回答3:
Try this,
Live Demo
$('.ms-list-addnew .ms-herocl')[0].nextSibling.nodeValue = ''
回答4:
Use
$('#idHPNewDocument')[0].nextSibling.nodeValue = null;
来源:https://stackoverflow.com/questions/20003351/remove-text-from-html-node-with-jquery-javascript