为什么这个功能不起作用

Mah*_*sod -1 html javascript

我想将字符串转换为整数.我知道有内置函数可以做到,但我仍然想知道为什么这个函数不起作用:
JS: - 保存为js1.js

function atoi(str)
 {
  l = str.length;
  s2 = "0"
  for(i=0;i<l;i++)
   {
    if(str.charAt(i) != '1' || str.charAt(i) != '2' || str.charAt(i) != '3' || str.charAt(i) != '4' || str.charAt(i) != '5' || str.charAt(i) != '6' || str.charAt(i) != '7' || str.charAt(i) != '8' || str.charAt(i) != '9' || str.charAt(i) != '0')
     {
        break;
     }
     s2 = s2.concat(str.charAt(i));
   }
  return Number(s2); 
 }
Run Code Online (Sandbox Code Playgroud)

HTML:

<html>
 <head>
  <script src="js1.js">
  </script>
  <Script>
    function printnum()
     {
      n = atoi(document.getElementById('numtxt').value)
      document.write(n);
     }
  </script>
  <title>
    Test JS1 functions
  </title>
 </head>
 <body>
  <input type="text" id="numtxt">
  <input type="button" onclick="printnum()">
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

谢谢.

Que*_*tin 5

你放弃,break如果第一个字符不是a 1 或者它不是a 2,等等.

如果它是1那么它不是2和你break.

你想使用&&没有||.