简体   繁体   English

Javascript可内联,但不能在文件中

[英]Javascript works inline but not in a file

I am new to javascript and am assuming that the answer is simple and right under my nose, but I can't seem to find the proverbial dog that's going to bite me. 我是javascript的新手,并认为答案很简单,就在我的鼻子底下,但我似乎找不到能咬我的谚语的狗。

Anyway, this works inline: 无论如何,这可以内联:

<script language="javascript">
    function foo(){

    alert("BOO!")

    }
</script>

<body onload=foo()>...

But if I do: 但是,如果我这样做:

<script type="text\javascript" scr="js/blah.js">
</script>

<body onload=foo()>

blah.js contains: blah.js包含:

function foo(){

    alert("BOO!")

    }

That doesn't work. 那不行

Why? 为什么?

正确的语法是:

<body onload="foo();">

The type is text/javascript ( / instead of \\ ), and the onload attribute in the body element should be onload="foo();" 类型为text/javascript/代替\\ ),并且body元素中的onload属性应为onload="foo();" .

Also, is your blah.js file stored inside a directory called js? 另外,您的blah.js文件是否存储在名为js的目录中?

Inside markup you can call JavaScript functions. 在标记内部,您可以调用JavaScript函数。 There is nothing wrong with it. 没有什么问题。 This creates a new anonymous function in the DOM tree and adds the value of the Event model as the function body. 这将在DOM树中创建一个新的匿名函数,并将Event模型的值添加为函数主体。

<script type="text\javascript" scr="js/blah.js"> </script>

should be 应该

<script type="text/javascript" src="js/blah.js"> </script> // / and src

should be 应该

<body onload="foo()"> // Markup attribute values should be passed inside quotes

所有属性值都必须以“开头”,并以“结尾”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM