2016-04-14 60 views
0

使用javascript创建我的第一个计算器。我的JavaScript代码不工作,但JavaScript文件通过。当它全部在HTML文件中工作,但不是现在。看起来一切都很好。有什么问题吗?谢谢javascript未启用

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

<html> 
    <head> 
    <script src="javascript/calc.js"></script> 
    <link href="css/calc.css" rel="stylesheet"> 
    <TITLE>Calculator</TITLE> 
    <style> 
    </style> 
    </head> 
<body> 
     <div id="calc-container"> 
     <form name="calculator"> 
     <input type="text" onkeyup="return proverka(this);" autofocus maxlength="10" placeholder="Введіть число" name="answer" /> 
     <br /> 

     <input type="button" value=" 1 " onclick="calculator.answer.value += '1'" /> 
     <input type="button" value=" 2 " onclick="calculator.answer.value += '2'" /> 
     <input type="button" value=" 3 " onclick="calculator.answer.value += '3'" /> 
     <input type="button" value=" + " onclick="calculator.answer.value += '+'" /> 
     <br /> 

     <input type="button" value=" 4 " onclick="calculator.answer.value += '4'" /> 
     <input type="button" value=" 5 " onclick="calculator.answer.value += '5'" /> 
     <input type="button" value=" 6 " onclick="calculator.answer.value += '6'" /> 
     <input type="button" value=" - " onclick="calculator.answer.value += '-'" /> 
     <br /> 

     <input type="button" value=" 7 " onclick="calculator.answer.value += '7'" /> 
     <input type="button" value=" 8 " onclick="calculator.answer.value += '8'" /> 
     <input type="button" value=" 9 " onclick="calculator.answer.value += '9'" /> 
     <input type="button" value=" &#215; " onclick="calculator.answer.value += '*'" /> 
     <br /> 

     <input type="button" value=" C " onclick="calculator.answer.value = ''" /> 
     <input type="button" value=" 0 " onclick="calculator.answer.value += '0'" /> 
     <input type="button" value=" = " onclick="calculator.answer.value = eval(calculator.answer.value)" /> 
     <input type="button" value=" &#247; " onclick="calculator.answer.value += '/'" /> 
     <br /> 

     <input type="button" value=" exp " onclick="calculator.answer.value = Math.exp(calculator.answer.value);" /> 
     <input type="button" value=" ln " onclick="calculator.answer.value = Math.log(calculator.answer.value);" /> 
     <input type="button" value=" &#8730; " onclick="sqrt(calculator.answer.value)" /> 
     <input type="button" value=" x&#178; " onclick="sqr(calculator.answer.value)" /> 
     <br /> 

     <input type="button" value=" cos " onclick="calculator.answer.value = Math.cos(calculator.answer.value);" /> 
     <input type="button" value=" sin " onclick="calculator.answer.value = Math.sin(calculator.answer.value);" /> 
     <input type="button" value=" tan " onclick="calculator.answer.value = Math.tan(calculator.answer.value);" /> 
     <input type="button" value=" &larr; " onclick="backspace(calculator.answer.value);"; /> 
     <br /> 

     <p align=right>&#8226;Sergiy Starshoy &#169;</p> 
     </form> 
    </div> 
</body> 
</html> 






<!DOCTYPE html> 
<html> 
<head> 
<script> 

function proverka(input) 
{ 
    var value = input.value; 
    var rep = /[-;":'а-яА-ЯёЁa-zA-ZA\\=`ё/\*[email protected]#$%\^&_№?><]/; 
    var rep2 = /а|б|в|г|д|е|ё|ж|і|з|и|ё|к|л|м|н|о|п|р|с|т|у|ф|х|ц|ч|ш|щ|ъ|ы|ь|э|ю|я/gi; 

     if (rep.test(value)) 
     { 
     value = value.replace(rep, ''); 
     input.value = value; 

      if (rep2.test(value)) 
      { 
       value = value.replace(rep2, ''); 
       input.value = value; 
      } 
     } 

} 

function backspace(value) 
{ 
calculator.answer.value = calculator.answer.value.substring(0, calculator.answer.value.length - 1) 
return input.value; 
} 

function sqrt(value) 
{ 
calculator.answer.value = Math.sqrt(value); 
} 

function sqr(value) { 
calculator.answer.value = (calculator.answer.value)*(calculator.answer.value); 
} 

</script> 
</head> 
<body> 
</body> 
</html> 
+0

从脚本文件中删除HTML,只保留javascript代码。 – icke

+0

“当它全部在html文件中工作,但不是现在”它现在在哪里? – atmd

回答

-1

为什么要重新定义整个HTML代码? 在关闭<body>标签之前,将您的Javascript代码放入<script></script>标签。

例:

<!DOCTYPE html> 
<html> 
    <head> 
     <script src="..."></script> 
    </head> 
    <body> 
     .... 

     <script> 
      // Here some JS code 
     </script> 
    </body> 
</html> 
3

,因为你添加的文件中纯HTML您的JavaScript文件不起作用。您需要删除实际JavaScript代码周围的HTML标签。

基本上,.js文件需要脚本标记之间的所有代码,而不需要其他任何代码。

0

如果它的工作原理,当你有你的JavaScript内的HTML,但不是在外部文件,那么可能你有错误的路径js文件。如果你的项目文件夹内有2个文件夹(一个带有html文件,另一个带有Js文件),然后在你的路径 与../你上你的文件夹一级,然后进入你的JavaScript文件夹找到文件