zoukankan      html  css  js  c++  java
  • 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>标签中编写脚本完全一致。

  • 相关阅读:
    qt程序编译错误:could not exec ‘/usr/lib/x86_64-linux-gnu/qt4/bin/qmake’
    安装 yaml-cpp,MP4V2
    安装cmake 和 opencv 4.0.0
    windows系统,boost编译安装
    messageQ 消息队列
    fflush 和 fsync 的区别
    开源一个 PDF 小工具集软件【使用 PDFium 库实现】
    封装 libjpeg 库
    纯 C++ 代码实现的 INI 文件读写类
    C++11 —— 使用 thread 实现线程池
  • 原文地址:https://www.cnblogs.com/panpy/p/7787948.html
Copyright © 2011-2022 走看看