1. 程式人生 > >獲取字符串出現的次數

獲取字符串出現的次數

innerhtml tar htm doctype tag () click button document

<!DOCTYPE html> <html> <body>
<p id="p1">start stop speed start speed velociting start</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script> function myFunction() { var str = "start stop speed start speed velociting start"; var pos = str.indexOf("start"); var time = 0; var sp =str.indexOf("start") ; while(sp != -1) { sp = str.indexOf("start",sp); if(sp !=-1) {time++; sp++; } } document.getElementById("demo").innerHTML = pos; document.getElementById("demo").innerHTML =time;
}
<p>Hello world!</p>
var x=document.getElementsByTagName("p"); document.getElementById("p1").innerHTML=‘The ....is‘+x[0].innerHTML;
</script>
</body> </html>

獲取字符串出現的次數