可能重复:
jQuery查找并替换字符串
这是我的HTML:
<div class="dt_lft">There is an error</div>
Run Code Online (Sandbox Code Playgroud)
我需要找到div是否有文本error,并success使用类名替换该文本.这该怎么做?
您可以使用:contains()选择器:
$("div.dt_lft:contains('error')").text(function(i, text) {
return text.replace('error', 'success');
});
Run Code Online (Sandbox Code Playgroud)
如果字符串error可能在文本中出现多次,则需要使用RegEx来处理:
$("div.dt_lft:contains('error')").text(function(i, text) {
return text.replace(new RegExp('error', 'g'), 'success');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
443 次 |
| 最近记录: |