html页面中有一段文本"function say(){ ... }",我想将里面的"function"字符串用红色字体突出显示出来
1 回答
精慕HU
TA贡献1845条经验 获得超8个赞
最近在做一个编辑器应用,用到了正则,相当于匹配关键字吧:
<html><head>
<meta charset="utf-8"/>
<title>Test</title>
<style>
.red{ color:red; } </style></head><body><p>function say(){...}</p><script>
var p = document.getElementsByTagName('p')[0]; var txt = p.innerHTML;
txt = txt.replace(/function/g, '<span class="red">function</span>');
p.innerHTML = txt;</script></body></html>添加回答
举报
0/150
提交
取消
