zoukankan      html  css  js  c++  java
  • javascript常用方法总结

     

    1、JavaScript:写入 HTML 输出

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

    2、JavaScript:对事件作出反应

    1 <button type="button" onclick="alert('Welcome!')">点击这里</button>

    3、JavaScript:改变 HTML 内容

    1 x=document.getElementById("demo") //查找元素
    2 x.innerHTML="Hello JavaScript"; //改变内容

    4、JavaScript:改变 HTML 图像

    1 element=document.getElementById('myimage')
    2 element.src="../i/eg_bulboff.gif";

    5、改变 HTML 样式

    1 x=document.getElementById("demo") //找到元素
    2 x.style.color="#ff0000"; //改变样式

    6、JavaScript 对大小写敏感。

    JavaScript 对大小写是敏感的。
    当编写 JavaScript 语句时,请留意是否关闭大小写切换键。
    函数 getElementById 与 getElementbyID 是不同的。
    同样,变量 myVariable 与 MyVariable 也是不同的。

    7、提示:一个好的编程习惯是,在代码开始处,统一对需要的变量进行声明。

    8、Value = undefined

    在计算机程序中,经常会声明无值的变量。未使用值来声明的变量,其值实际上是 undefined。在执行过以下语句后,变量 carname 的值将是 undefined:
    var carname;

    9、创建 JavaScript 对象
    本例创建名为 "person" 的对象,并为其添加了四个属性:

    1 person=new Object();
    2 person.firstname="Bill";
    3 person.lastname="Gates";
    4 person.age=56;
    5 person.eyecolor="blue";

    10、JavaScript 表单验证

    必填(或必选)项目
    下面的函数用来检查用户是否已填写表单中的必填(或必选)项目。假如必填或必选项为空,那么警告框会弹出,并且函数的返回值为 false,否则函数的返回值则为 true(意味着数据没有问题):

     1 <html>
     2 <head>
     3 <script type="text/javascript">
     4 
     5 function validate_required(field,alerttxt)
     6 {
     7 with (field)
     8 {
     9 if (value==null||value=="")
    10 {alert(alerttxt);return false}
    11 else {return true}
    12 }
    13 }
    14 
    15 function validate_form(thisform)
    16 {
    17 with (thisform)
    18 {
    19 if (validate_required(email,"Email must be filled out!")==false)
    20 {email.focus();return false}
    21 }
    22 }
    23 </script>
    24 </head>
    25 
    26 <body>
    27 <form action="submitpage.htm" onsubmit="return validate_form(this)" method="post">
    28 Email: <input type="text" name="email" size="30">
    29 <input type="submit" value="Submit"> 
    30 </form>
    31 </body>
    32 
    33 </html>
  • 相关阅读:
    HDU 2236 无题II
    P2220 [HAOI2012]容易题
    UVA11383 Golden Tiger Claw
    AT2272 [ARC066B] Xor Sum
    CentOS7 静默安装oracle12c
    SNAT与DNAT
    Linux下离线安装Docker
    TJOI2017 DNA 和 BJOI2015 隐身术
    LOJ6169 相似序列
    BJOI2019 删数
  • 原文地址:https://www.cnblogs.com/19322li/p/10849374.html
Copyright © 2011-2022 走看看