数组 a ,用for循环或者forEach循环 ,然后用replace把某个词用红色代替 。
父级 . replace(被替换的字,新的字)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
onload=function(){
var a=["小红","小黄","小蓝","11","4","7777"];
// for (var i=0; i<a.length; i++) {
// var ch=a[i];
// var reg = "/"+ch+"/g";
// document.body.innerHTML = document.body.innerHTML.replace(eval(reg),"<span style='color: red;'>$&</span>");
// }
a.forEach(function(item , index){
console.log(item);
// console.log(index)
var ch=item;
var reg = "/"+ch+"/g";
document.body.innerHTML = document.body.innerHTML.replace(eval(reg),"<span style='color: red;'>$&</span>");
})
}
</script>
</head>
<body>
<div>小红</div>
<div>小蓝</div>
<div>小黑</div>
<div>小红</div>
<div>7777</div>
</body>
</html>