• 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

  • 相关阅读:
    记一次安装python umysql模块的报错
    elasticsearch 6.2.4添加用户密码认证
    mysqldump 备份数据和恢复
    记一次线上Java程序导致服务器CPU占用率过高的问题排除过程
    配置rpm包安装的jdk环境变量
    centos6 & centos 7 防火墙设置
    MySQL启动出现The server quit without updating PID file错误解决办法
    ptmalloc,tcmalloc和jemalloc内存分配策略研究 ? I'm OWen..
    为什么要内存对齐 Data alignment: Straighten up and fly right
    linux驱动学习_1
  • 原文地址:https://www.cnblogs.com/qinxuemei/p/3970453.html
Copyright © 2020-2023  润新知