<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head>
<body>
<input type="text" />
<input type="button" value="检测" />
<script>
// alert( detectNum('123456') );
var aInp = document.getElementsByTagName('input');
/*aInp[1].onclick = function () {
var val = aInp[0].value;
if ( detectNum(val) ) {
alert( '恭喜,'+ val +'全是数字' );
} else {
alert('输入有误');
}
};
*/
aInp[1].onclick=function(){
var val=aInp[0].value;
console.log(val,"输入的数字")
alert(detectNum(val) )
}
function detectNum ( str ) {
var n = 0;
for ( var i=0; i<str.length; i++ ) {
n = str.charCodeAt(i);
console.log(i)
if ( n<48 || n>57 )return "不是数字";//在return之后代码不执行,是不是指return之后不单单是这个for循环不在执行,包括这个函数本身也是不再执行的?
}
return true;
}
</script>
</body>
</html>
在一个函数作用域中如果使用了return,其后面的代码不会执行。for循环并不能形成一个作用域所以整个函数都不会再执行