<p>some string here xxx</p>
Run Code Online (Sandbox Code Playgroud)
我想删除xxx字符
所以我这样做$(p:contains('xxx')).remove();却删除了整个<p></p>,如何摆脱目标角色?
那将是text方法和字符串replace
$('p').text(function(_, txt) {
return txt.replace('xxx', '');
});
Run Code Online (Sandbox Code Playgroud)
remove 删除整个元素,而不是文本的一部分.
这是预期的行为remove(),它删除了整个元素.您可以使用text(fn)或html(fn)执行此操作.
$("p:contains('xxx')").text(function(idx, oldText){
return oldText.replace('xxx','');
});
Run Code Online (Sandbox Code Playgroud)
如果您在p使用中还有其他标签html(fn),如上所述
| 归档时间: |
|
| 查看次数: |
76 次 |
| 最近记录: |