javascript未处于活动状态
javascript is not active
本文关键字:活动状态 javascript 更新时间:2024-06-16
在javascript上制作我的第一个计算器。我的javascript代码不起作用,但javascript文件由链接。当它都在html文件中时,它起作用了,但现在不行了。看起来一切都很好。什么问题?谢谢
<!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="answerswer" />
<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''=`ё/'*++!@#$%'^&_№?><]/;
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>
您的JavaScript文件无法工作,因为您在文件中添加了纯HTML。您需要删除实际JavaScript代码周围的HTML标记。
基本上,.js文件需要脚本标记之间的所有代码,而不需要其他代码。
如果javascript在html中,但不在外部文件中,则可能是js文件的路径错误。如果您的项目文件夹中有2个文件夹(一个包含html文件,一个包含Js文件),则在您的路径上带有../在文件夹上向上一级,然后进入javascript文件夹,找到文件
为什么要重新定义整个HTML代码?在关闭<body>
标记之前,将您的Javascript代码放入<script></script>
标记中。
示例:
<!DOCTYPE html>
<html>
<head>
<script src="..."></script>
</head>
<body>
....
<script>
// Here some JS code
</script>
</body>
</html>
相关文章:
- 使用纯javascript而非jquery使所选选项卡处于活动状态并保持非活动状态
- 返回上一页时,Javascript仍处于活动状态
- 限制 .live() 函数在 JavaScript 中变为活动状态的次数
- javascript未处于活动状态
- Javascript/JQuery样式-#按钮:点击全屏后悬停保持活动状态
- Javascript:在表单中输入数据时,提交按钮不处于活动状态
- 查看CSS:invalid选择器当前是否在JavaScript中处于活动状态
- Javascript 仅在从特定目录查看时处于活动状态
- JavaScript 下拉菜单在单击时处于活动状态,在外部单击时不活动
- 仅在一个窗口中播放声音,该窗口使用javascript(可能是jquery)处于活动状态(在一个网站内)
- 活动状态菜单 JavaScript
- 在 JavaScript 函数中保持变量处于活动状态时出现问题
- 如何在多页HTML5应用程序中保持JavaScript对象的活动状态
- 使链接保持活动状态,使用 JavaScript 在单击时显示悬停效果
- 有没有办法找出Javascript中有多少个间隔处于活动状态
- “Enter”键在文本框处于活动状态时调用javascript函数
- javascript菜单悬停处于活动状态
- 了解开发者控制台在当前浏览器(JavaScript)中是否处于活动状态
- javascript自动注销10分钟的非活动状态
- 检测浏览器窗口是否处于活动状态,并在窗口再次处于活动状态后启动事件[JavaScript]