HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./js练习.js">
<title>练习js</title>
</head>
<body>
<p id="p1">start,stop,speed,start,speed ,velocicty,start</p>
<button onclick="fun()">确定</button>
<p id="demo"></p>
<script src="./js练习.js"></script>
</body>
</html>
js
function fun(){
var str = document.getElementById("p1").innerHTML;
var num=0;
var a=str.indexOf("start");
while(a!==-1){
a=str.indexOf("start",a+1);
num++;
}
document.getElementById("demo").innerHTML = num;
}
实验结果:
start,stop,speed,start,speed ,velocicty,start