[英]How can I get this Script to run multiple Words
嘿,
我目前仍受脚本困扰。 我想删除HTML标签中无法更改其标记的特定单词。 我搜索了一个解决方案,并找到了以下脚本:
$(':contains("bad")').each(function(){
$(this).html($(this).html().split("bad").join(""));
});
现在此脚本可以很好地处理一个单词( bad
),但是我想用此脚本删除mutliple
单词。 所以我尝试了这个:
$(':contains("bad", "good")').each(function(){
$(this).html($(this).html().split("bad","good").join(""));
});
但这根本不起作用。
这是一个工作片段:
$(':contains("bad")').each(function(){ $(this).html($(this).html().split("bad").join("")); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>A beautiful badsentence</div>
我该如何运作?
只需.replace(/bad|good/g, '')
。 /g
表示替换所有匹配项,而不仅仅是第一个。 bad|good
方法将其中的任何一个替换掉。
$(document).ready(function () { $('#text').text($('#text').text().replace(/good|bad/g, '')); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span id="text">Some words are not as good or not as bad, but bad words may be good too.</span>
您可以尝试以下方法:
var replaced = $('#text').text().replace("good", '').replace("bad", '');
$('#text').text(replaced);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.