[英]What's wrong with this javascript code? (body script calling head function)
Why does the following not produce any result? 为什么以下结果没有任何效果? I get a blank page.
我得到空白页。 I kept modifying/simplifying the code to see where the problem is and it seems to be with the line
我不断修改/简化代码,以查看问题出在哪里,并且似乎与该行有关。
"var count = NbnamePattern(names)"
Things seem to work when the body script calls a function defined in the head but with no arguments passed. 当主体脚本调用在头中定义但未传递任何参数的函数时,事情似乎就起作用了。
<!DOCTYPE html> <html lang="en"> <head> <title>Assignment 2 Q4</title> <meta charset="utf-8" /> <script> function NbnamePattern(var names) { var count = 0; for (var i in names) { if (names[i].search(/ie$/) != -1 || names[i].search(/y$/) != -1) count++; } return count; } </script> </head> <body> <p></p> <script type="text/javaScript"> var names = new Array("freddie", "bob", "mieke", "yahoo2", "georgey"); var count = NbnamePattern(names); document.getElementsByTagName("p")[0].innerHTML = "The number of names having these two patterns (/ie$/) and (/y$) in the array is:" + count; </script> </body> </html>
function NbnamePattern(var names){
var count = 0;
for(var i in names)
if(names[i].search(/ie$/) != -1 || names[i].search(/y$/) != -1)
count++;
return count;
}
should be 应该
function NbnamePattern(names){
var count = 0;
for(var i in names)
if(names[i].search(/ie$/) != -1 || names[i].search(/y$/) != -1)
count++;
return count;
}
The functions in javascript dont take types, it should just be name javascript中的函数不接受类型,应该只是名称
you need to remove the var
from NbnamePattern(var names)
function 您需要从
NbnamePattern(var names)
函数中删除var
<!DOCTYPE html> <html lang="en"> <head> <title>Assignment 2 Q4</title> <meta charset="utf-8" /> <script> function NbnamePattern(names) { var count = 0; for (var i in names) { if (names[i].search(/ie$/) != -1 || names[i].search(/y$/) != -1) count++; } return count; } </script> </head> <body> <p></p> <script type="text/javaScript"> var names = new Array("freddie", "bob", "mieke", "yahoo2", "georgey"); var count = NbnamePattern(names); document.getElementsByTagName("p")[0].innerHTML = "The number of names having these two patterns (/ie$/) and (/y$) in the array is:" + count; </script> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.