<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<style>
span{
color: red;
}
</style>
</head>
<body>
<div id="div1">
</div>
<input type="text" />
<input type="text" />
<input type="button" value="查找"/>
<input type="button" value="替换"/>
<p>阿三大苏打撒旦撒大苏打顶顶顶顶顶顶打算阿斯顿撒旦撒阿三大苏打撒旦撒</p>
</div>
<script>
var aInp=document.getElementsByTagName('input');
var oP=document.getElementsByTagName('p')[0];
aInp[3].onclick=function(){
var str=aInp[0].value;
var newStr= aInp[1].value;
if(!str)return;
oP.innerHTML=oP.innerHTML.split(str).join('<span>'+newStr+'</span>');
}
aInp[1].onclick=function()
{
var str=aInp[0].value;
oP.innerHTML=oP.innerHTML.split(str).join('<span>'+str+'</span>');
}
</script>
</body>
</html>