Vic*_*tor 1 html javascript jquery
我在jQuery尝试删除<span>标签后的文本以附加其他文本时遇到了麻烦.
例:
<div id="foo-id">
<span id="span-id">
<i id="icon-id" class="fa fa-square-o"></i>
</span> text..here... (this text needs to be removed)
</div>
Run Code Online (Sandbox Code Playgroud)
我知道如何附加文字:
$('#foo-id').after()[0].append('this is a text');
Run Code Online (Sandbox Code Playgroud)
但是如何在</ span>标签后删除/清除文本以附加新文本?
假设#foo-id您内部没有其他文本节点可以执行以下操作:
$('#foo-id').html(function(){
return $(this).children(); //only return elements, not text nodes
}).append('Some new text')Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="foo-id">
<span id="span-id">
<i id="icon-id" class="fa fa-square-o"></i>
</span> text..here... (this text needs to be removed)
</div>Run Code Online (Sandbox Code Playgroud)