2016-04-14 73 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文件夾找到文件