eighth week(1)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>查詢字符串次數</title>
</head>
<body>
<button onclick="myFunction()">點擊查詢字符串中"first"字符出現的次數</button>
<p id="demo"></p>

<script type="text/javascript">
 function myFunction()
 {
     var str = "first,second,third,first,speed,first,flexible,first,third";
     var count=0;
     var index=0;
     var key="first";
     while((index = str.indexOf(key,index))!= -1)
     {
         index += key.length;
         count = count+1;
    }
    document.getElementById("demo").innerHTML = count;
}
</script>
</body>
</html>

javascript

 

注:本文借鑑自https://www.cnblogs.com/yyh187/p/9835689.htmlhtml

相關文章
相關標籤/搜索