无法在html中引用js文件

时间:2014-03-15 20:51:13

标签: javascript html

我是JavaScript的初学者。我想在HTML中引用js.function。以下是我的代码。当我运行它时,我收到错误:

  

jisuan未定义

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script language = "javascript",src ="myfuns.js"></script>
<script language = "javascript",type="text/javascript">
    var num1 = window.prompt("please input the first num");
    var num2 = window.prompt("please input the second num");
    var operator = window.prompt("please input an operator");
    num1= parseFloat(num1);
    num2= parseFloat(num2);
    document.writeln("the result is" + jisuan(num1,num2,operator));
</script>
</head>
<body>
</body>
</html>


function jisuan(num1,num2,operator)
{
    var res = 0;
    if(operator =='+')
    {
        res = num1 + num2;
    }else if(operator =='-')
    {
        res = num1 - num2;
    }else if(operator =='*')
    {
        res = num1*num2;
    }else if(operator =='/')
    {
        res = num1/num2;
    }
    return res;
}

3 个答案:

答案 0 :(得分:1)

如果你used a markup validator,你会收到很多错误。

造成问题的特定问题出在此处:

<script language = "javascript",src ="myfuns.js"></script>
                               ^

您需要在属性之间有一个或多个空格字符(例如空格),但是您有一个U + FF0C:FULLWIDTH COMMA()。这会导致无法识别src属性。

在HTML 5中,language属性无论如何都已过时,因此应将其删除。如果要指定您正在使用的脚本语言(只有在您不使用JavaScript时才应该这样做),那么(自1996年起)您应该使用type属性。

<script src="myfuns.js"></script>

使用a markup validator查找其余错误。

答案 1 :(得分:0)

您错过了="javascript",src"javascript",type

上的空格
<script language = "=javascript",src ="myfuns.js"></script>
//-----------there is no space-^    

<script language = "javascript",type="text/javascript">
//-----------there is no space-^    

但应该是

正如 Quentin 所指出的那样,也不应该使用逗号。

更新

<script language = "javascript" src ="myfuns.js"></script>
<script language = "javascript" type="text/javascript">

答案 2 :(得分:-1)

使用此

<script language = "javascript" type="text/javascript">

所以没有&#34;,&#34; 不是这个:

<script language = "javascript",type="text/javascript">

所以这个

 <script language = "javascript",src ="myfuns.js"></script>

也是这样的:

 <script language = "javascript" src ="myfuns.js"></script>

将所有javascript代码设置为:

<script language ="javascript">

</script>

所以这个

function jisuan(num1,num2,operator)
{
    var res = 0;
    if(operator =='+')
    {
        res = num1 + num2;
    }else if(operator =='-')
    {
        res = num1 - num2;
    }else if(operator =='*')
    {
        res = num1*num2;
    }else if(operator =='/')
    {
        res = num1/num2;
    }
    return res;
}

就是这个

<script language ="javascript">
function jisuan(num1,num2,operator)
{
    var res = 0;
    if(operator =='+')
    {
        res = num1 + num2;
    }else if(operator =='-')
    {
        res = num1 - num2;
    }else if(operator =='*')
    {
        res = num1*num2;
    }else if(operator =='/')
    {
        res = num1/num2;
    }
    return res;
}
</script>