您能看一下This Demo吗?让我知道如何循环遍历像 <pre>
这样的元素并替换所有 <
和>
一些新 Angular 色,例如:
.replace("<", "1");
.replace(">", "2");
如果我们有 <pre>
就像
<pre>
< This is a test < which must > replace >
</pre>
谢谢
最佳答案
您可以使用.text()和 String.replace()使用RegExp
$('pre').text(function(i, text){
return text.replace(/</g, '1').replace(/>/g, '2')
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<pre>
< This is a test < which must > replace >
</pre>
关于javascript - 如何循环和替换元素内的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903779/