Web作业:specific word count (index of )

统计文件中某一词语出现次数:
HTML:
    <p id="p1">start,stop,speed,start,speed ,velocicty,start</p>
    <button onclick="fun()">Try it</button>
    <p id="demo"></p>
    <script src="./index.js"></script>

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; }
posted @ 2018-11-03 23:22  卷公举  阅读(165)  评论(0编辑  收藏  举报