JavaScript学习笔记(1)

学习内容源自W3CSchool


JavaScript简介

  是一种轻量级的编程语言
  是可插入HTML页面的编程代码
  由浏览器执行
  简单易学

JavaScript的一些常见用途:

1 HTML输出:

  document.write("<h1>Hello,everyone!</h1>");

范例:
<!DOCTYPE html>
<html>
<body>

  <p>
    JavaScript 能够直接写入 HTML 输出流中:
  </p>

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

  <p>
    您只能在 HTML 输出流中使用 <strong>document.write</strong>。
    如果您在文档已加载后使用它(比如在函数中),会覆盖整个文档。
  </p>

  </body>
</html>

注意:只能在HTML输出中(网页加载前)使用document.write,而不能再文档加载后使用,否则会覆盖整个文档。


2 对事件作出反应

  <button type="button" onclick="alert('Welcome!')">Click here</button>


3 改变HTML内容

  x=document.getElementById("_id");  //查找元素

  x.innerHTML="Hello, JavaScript";    //改变内容


范例:
<!DOCTYPE html>

<html>

<body>

  <p id="demo">JavaScript is very easy to learn and use...</p>

  <script>

    function myFunction()

    {

      //document.write("hehe"); //这句代码会重写网页的整个内容

      x=document.getElementById("demo");

      x.innerHTML="Thank you for sharing...";

    }

  </script>

  <button type="button" onclick="myFunction()">Click hear</button>

</body>

</html>

4 改变HTML图片

  x=document.getElementById("pic01");

  x.style.color="#ff0000";

<!DOCTYPE html>
<html>
<body>

  <h1>我的第一段 JavaScript</h1>

  <p id="demo">
    JavaScript 能改变 HTML 元素的样式。
  </p>

  <script>
    function myFunction()
    {
      x=document.getElementById("demo") // 找到元素
      x.style.color="#ff0000"; // 改变样式
    }
  </script>

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

</body>
</html>

5 验证输入

  if isNaN(x) {alter("Not Numeric")};

<!DOCTYPE html>
<html>
<body>

  <h1>我的第一段 JavaScript</h1>

  <p>请输入数字。如果输入值不是数字,浏览器会弹出提示框。</p>

  <input id="demo" type="text">

  <script>
    function myFunction()
    {
      var x=document.getElementById("demo").value;
      if(x==""||isNaN(x))
      {
        alert("Not Numeric");
      }
    }  
  </script>

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

</body>
</html>


JavaScript脚本语言的使用

  在HTML中,脚本语言必须放置在<script>和</script>标签中。

  如:

<script>
alert("My First JavaScript");
</script>


JavaScript函数和事件

  通常,我们需要在某个事件发生时执行代码,如用户点击按钮。

  如果我们把JavaScript代码放入函数中,就可以在事件发生时调用该函数。

 

<head>或<body>标记中的JavaScript

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

  JavaScript脚本可以位于HTML中的<body>或<head>标记中

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

(1)<head>中的JavaScript

<!DOCTYPE html>
<html>

<head>
  <script>
  function myFunction()
  {
    document.getElementById("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>

(2)<body>标记中的JavaScript

<!DOCTYPE html>
<html>
<body>

  <h1>My Web Page</h1>

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

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

  <script>
    function myFunction()
    {
      document.getElementById("demo").innerHTML="My First JavaScript Function";
    }
  </script>

</body>
</html>

外部的JavaScript

  更多的时候,可以把脚本保存在外部文件中,外部文件通常包含被多个网页使用的代码

  外部JavaScript文件的扩展名是.js

  如需使用外部文件,需要在<script>标签的“src”属性中设置该“.js”文件

<!DOCTYPE html>
<html>
<body>

  <h1>My Web Page</h1>

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

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

  <script>
    function myFunction()
    {
      document.getElementById("demo").innerHTML="My First JavaScript Function";
    }
  </script>

</body>
</html>


以上为JavaScript的一些简单介绍和使用方法。

原文地址:https://www.cnblogs.com/chenliangwen/p/4421494.html