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=" &#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''=`ё/'*++!@#$%'^&_№?><]/;
    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>