使用 javascript 提示我得到两个参数,如搜索字符串和搜索关键字,然后搜索关键字并获取找到的元素数。 然后需要在页面上显示它们。似乎是我犯的错误。
<html>
<head>
<script type = "text/javascript">
var counter = 0;
var enter = prompt("Enter your String:");
var search = prompt("Enter words to search:");
var b = search.length;
var a = enter.length - search.length;
for (var y = 0; y <= a; y++)
{
if(b <= enter.length){
if(enter.substring(y,B))
{
counter = counter + 1;
}
b++;
}
else{
document.write("<p>" + "ERROR" + "</p>");
}
document.write("<p>" + "your word:" + enter + "</p>");
document.write("<p>" + "word use:" + counter + "</p>");
</script>
<body>
</body>
</head>
</html>
最佳答案
- 您正在使用未声明的变量。请记住,Javascript 区分大小写并且变量 b 不同于 B。
- 您忘记关闭 for 括号。
- 您需要将子字符串与搜索模式进行比较。
关于javascript - 计算在字符串中搜索的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9226901/