Remove text from html node with jQuery/Javascript

穿精又带淫゛_ 提交于 2021-01-28 20:17:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!