<!DOCTYPE html>
<html>
<head>
<title>查找</title>
<meta charset="utf-8">
</head>
<body>

<p>吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮</p>
<button onclick="find()">点击查看"葡萄"这个词出现的次数</button>

<p id="demo"></p>

<script>
function find(){


var str="吃葡萄不吐葡萄皮,不吃葡萄倒吐葡萄皮"
var y="葡萄";
var count=0;
var index=0;
for(z=0;z<=str.length;z++)
{
if(str.indexOf(y,index)!=-1)
{
index=str.indexOf(y,index)+y.length;
count=count+1;
}
}
document.getElementById("demo").innerHTML =count;
}
</script>

</body>
</html>

posted on 2018-10-29 18:17  菜鸟萌新  阅读(78)  评论(0编辑  收藏  举报