1. 程式人生 > >8th week blog 1

8th week blog 1

查找 element () button oct != index char innerhtml

<!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>

8th week blog 1