使用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=" × " 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=" ÷ " 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=" √ " onclick="sqrt(calculator.answer.value)" />
<input type="button" value=" x² " 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=" ← " onclick="backspace(calculator.answer.value);"; />
<br />
<p align=right>•Sergiy Starshoy ©</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>
從腳本文件中刪除HTML,只保留javascript代碼。 – icke
「當它全部在html文件中工作,但不是現在」它現在在哪裏? – atmd