<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>替换任意指定的文本</title>
<style type="text/css">
div{
margin-left: 400px;
margin-top: 200px;
border: #4CAF50 30px groove;
text-align: center;
padding-bottom: 20px;
width: 1000px;
}
</style>
</head>
<body>
<div>
<p>替换“mountian”为“rain”</p>
<button onclick="replaceFunction()">点击这里</button>
<p id = "demo">hello mountain!</p>
</div>
<script>
function replaceFunction(){
var str = document.getElementById("demo").innerHTML;
var txt = str.replace("mountain","rain");
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
点击按钮: