找到文字进行标记(replaceWith)

时间:2023-12-13 16:35:02
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="./lib/d3.js"></script>
<script src="./lib/jquery.js"></script>
<style>
.red{
color:red;
}
</style>
</head>
<body>
<div>你我他</div>
<div>你我他</div>
<div>你我他</div>
<div>你我他</div> <script>
$('div').each(function(){
var $this = $(this);
var text = $this.text();
var start = '你';
var num = text.indexOf(start);
console.log(num);
if(num != -1){
text =text.replace(start,'<b class ="red">wo</b>');
$this.html(text);
}; });
setTimeout(function(){
$('.red').each(function(){
var $this = $(this);
var text = $this.text();
$this.replaceWith(text);
})
},3000)
</script>
</body>
</html>

replaceWith();将选中的标签包括标签进行替换。