JavaScript 使用

一、HTML中的脚本必须位于<script>和</script>之间

例如:<script>

    alert("My First JavaScipt ");

    </script>

二、<body>中的Javascript

实例:

<!DOCTYPE html>

<html>

<body>

<script>

document.write("<h1>This is a heading</h1>");

document.write("<p>This is a paragraph</p>");

</script>

</body>

</html>

三、<head>或<body>中的JavaSript

1、可以在HTML文档中放入不限数量的脚本

2、脚本可位于HTML的<body>或<head>部分中,或者同时存在于两个部分中

3、通常做法是把函数放入<head>部分中,或者放在页面底部。这样就可以把它们安置到同一处位置,不会干扰页面的内容。

四、<head>中的Javascript函数

实例:

<!DOCTYPE html>

<html>

<head>

<script>

function myFunction(){

  document.get.ElementById("demo").innerHTML="My First JavaScript Function";

}

</script>

</head>

<body>

<h1>My Web Page</h1>

<p id="demo">A Paragraph</p>

<button type="button" onclick="myFunction()">Try it</button>

</body>

</html>

五、<body>中的Javascript函数

实例:

<!DOCTYPE html>

<html>

<body>

  <h1>My Web Page</h1>

  <p id="demo">A Paragraph</p>

  <button type="button" onclick="myFuntion()">My First Javascript Function</button>

<script>

  function myFunction(){

    document.getElementById("demo").innerHTML="My First Javascript Function";

} 

</script>

</body>

</html>

 六、外部的Javascript

<!DOCTYPE html>

<html>

<body>

  <h1>My Web Page</h1>

  <p id="demo">A Paragraph.</p>

  <button type="button" onclick="myFunction()">点击这里</button>

  <p><b>注释:</b>myFunction 保存在名为“myscript.js”的外部文件中。</p>

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

</body>

</html>

在<head>或<body>中引用脚本文件都是可以的,实际运行效果与在<script>标签中编写脚本完全一致。

posted @ 2017-11-05 17:41  Panpy_Y  阅读(146)  评论(0编辑  收藏  举报