zoukankan      html  css  js  c++  java
  • JavaScript document 对象

    1.document属性

    • cookie -- 用户cookie
    • title -- 当前页面title标签中定义的文字
    • URL -- 当前页面的URL

    document代表HTML文档的内容,因此可以通过它表示文档中加载的一些元素,这些元素全部通过集合访问。

    • anchors -- 文档中所有锚(a name="aname")的集合
    • applets -- 文档中所有applet标签表示的内容的集合
    • embeds -- 文档中所有embed标签表示的内容的集合
    • forms -- 文档中所有form标签表示的内容的集合
    • images -- 文档中所有image标签表示的内容的集合
    • links -- 文档中所有a(链接)标签表示的内容的集合

    2.document函数

       a. document.write -- 在文档中写入字符串

       write函数语法

      document.write(str);

       write函数参数

    • str -- 要写入文档中的字符串

      b.document.writeln -- 在文档中写入字符串,并在字符串的末尾增加一个换行符

      c.document.open -- 打开已经载入的文档  

       示例

      var win = window.open("about:blank","dreamdu");

      win.document.open();

      win.document.write("welcome to dreamdu!");

      win.document.close();

      首先新建一个空白文档,并打开open,写入内容,最后完成显示关闭文档close。

       document.open函数语法

       window.document.open();

     

     d.document.close -- 用于关闭document.open方法打开的文档

       document.close函数语window.document.close();


    3.
    使用document索引页面内的元素

        可以使用数字或名称索引页面中的元素集合,每个元素的属性都变成了集合中相应对象的属性。

    示例

    <form name="form1"><a href="http://www.dreamdu.com/xhtml/" name="a1">xhtml</a></form>
    <form name="form2"><a href="http://www.dreamdu.com/css/" name="a2">css</a></form>
    <form name="form3"><a href="http://www.dreamdu.com/javascript/" name="a3">javascript</a></form>
    
    <input type="button" value="显示第二个表单的名称" onclick="alert(document.forms[1].name)" />
    <input type="button" value="显示第二个表单的名称第二种方法" onclick="alert(document.forms['form2'].name)" />
    <input type="button" value="显示第三个链接的名称" onclick="alert(document.links[2].name)" />
    <input type="button" value="显示第三个链接的名称第二种方法" onclick="alert(document.links['a3'].name)" />
    <input type="button" value="显示第三个链接href属性的值" onclick="alert(document.links[2].href)" />
    

    表示第二个表单的方法:document.forms[1]或document.forms["form2"]

    表示第三个链接的方法:document.links[2]或document.links["a3"]

    表示第三个链接href属性的方法:document.links[2].href

  • 相关阅读:
    Navicat for MySQL下载、安装与破解
    javaweb之Cookie学习
    static特别用法【静态导包】——Java包的静态导入
    面试感悟----一名3年工作经验的程序员应该具备的技能
    致孩子
    java中的代码块是什么意思,怎么用
    ModelAttribute用法之一
    SpringMVC获取页面数据乱码的解决get/post
    总结过去10年的程序员生涯 (经验)---大神的建议
    hdu 5237 二进制
  • 原文地址:https://www.cnblogs.com/qinxuemei/p/3970453.html
Copyright © 2011-2022 走看看