前面的话

  文档节点document,隶属于表示浏览器的window对象,它表示网页页面,又被称为根节点。本文将详细介绍文档节点document的内容

特征

  文档节点的三个node属性——nodeType、nodeValue、nodeName分别是9、'#document'和null

  由于它是根节点,所以其父节点parentNode指向null,ownerDocument也指向null

console.log(document.nodeType);//
console.log(document.nodeValue);//null
console.log(document.nodeName);//'#document'
console.log(document.parentNode);//null
console.log(document.ownerDocument);//null

快捷访问

子节点

【1】<html>

  document.documentElement属性始终指向HTML页面中的<html>元素 

console.log(document.documentElement.nodeName);//'HTML'

【2】<body>

  document.body属性指向<body>元素

console.log(document.body.nodeName);//'BODY'

【3】<!DOCTYPE>

  document.doctype属性指向<!DOCTYPE>标签

  [注意]IE8-不识别,输出null,因为IE8-浏览器将其识别为注释节点

console.log(document.doctype.nodeName);//'html'

【4】<head>

  document.head属性指向文档的<head>元素

  [注意]IE8-浏览器下不支持

console.log(document.head.nodeName);//'HEAD'

文档信息

【1】title

  <title>元素显示在浏览器窗口的标题栏或标签页上,document.title包含着<title>元素中的文本,这个属性可读可写

console.log(document.title);//Document
document.title="test";
console.log(document.title);//test

【2】URL、domain、referrer

  URL:页面的完整地址

  domain:domain与URL是相互关联的,包含页面的域名

  referrer:表示链接到当前页面的上一个页面的URL,在没有来源页面时,可能为空

  [注意]上面这些信息都来自请求的HTTP头部,只不过可以通过这三个属性在javascript中访问它而已

console.log(document.URL);//http://www.cnblogs.com/xiaohuochai/
console.log(document.domain);//www.cnblogs.com
console.log(document.referrer);//http://home.cnblogs.com/followees/

  在这3个属性中,只有domain是可以设置的。但由于安全方面的限制,也并非可以给domain设罝任何值。如果URL中包含一个子域名,例如home.cnblogs.com,那么就只能将domain设置为"cnblogs.com"。不能将这个属性设置为URL中不包含的域

document.domain = 'cnblogs.com';//"cnblogs.com"
//Uncaught DOMException: Failed to set the 'domain' property on 'Document': 'qq.com' is not a suffix of 'cnblogs.com'
document.domain = 'qq.com';

【3】baseURI

  document.baseURI返回<base>标签中的URL,如果没有设置<base>,则该值与document.URL相同

console.log(document.baseURI);'//http://www.cnblogs.com/xiaohuochai/'

<base href="http://www.baidu.com">
<script>
console.log(document.baseURI);//'http://www.baidu.com/'
</script>

【4】字符集charset

  document.charset表示文档中实际使用的字符集

console.log(document.charset);//'UTF-8'

【5】defaultView

  document.defaultView保存着一个指针,指向拥有给定文档的窗口或框架。IE8-浏览器不支持defaultView属性,但IE中有一个等价的属性名叫parentWindow。所以要确定文档的归属窗口,其兼容写法为:  

var parentWindow = document.defaultView || document.parentWindow;//Window

【6】兼容模式compatMode

  document.compatMode表示文档的模式,在标准模式下值为"CSS1Compat",在兼容模式下值为"BackCompat"

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
console.log(document.compatMode)//CSS1Compat
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
console.log(document.compatMode)//BackCompat
</script>
</body>
</html>

【7】文档模式documentMode

  document.documentMode属性表示当前的文档模式

  [注意]该属性只有IE11-浏览器支持

//IE11返回11,IE10返回10,IE9返回9,IE8返回8,IE7返回7,IE6返回6
console.log(document.documentMode);

【8】时间戳lastModified

  document.lastModified属性返回当前文档最后修改的时间戳,格式为字符串

console.log(document.lastModified); //09/02/2016 15:36:15

节点集合

【1】anchors

  document.anchors包含文档中所有带name特性的<a>元素

<a href= "#" name="a1">a1</a>
<a href= "#" name="a2">a2</a>
<a href= "#" >3</a>
<script>
console.log(document.anchors.length)//2
</script>

【2】links

  document.links包含文档中所有带href特性的<a>元素

<a href="#">1</a>
<a href="#">2</a>
<a>3</a>
<script>
console.log(document.links.length)//2
</script>

【3】forms

  document.forms包含文档中所有的<form>元素,与document.getElementsByTagName("form")结果相同

<form action="#">1</form>
<form action="#">2</form>
<script>
console.log(document.forms.length)//2
</script>

【4】images

  document.images包含文档中所有的<img>元素,与document.getElementsByTagName('img')结果相同

<img src="#" alt="#">
<img src="#" alt="#">
<script>
console.log(document.images.length)//2
</script>

【5】scripts

  document.scripts属性返回当前文档的所有脚本(即script标签)

<script>
console.log(document.scripts.length)//1
</script>

  以上五个属性返回的都是HTMLCollection对象实例

  [注意]关于HTMLCollection等动态集合的详细信息移步至此

console.log(document.links instanceof HTMLCollection); // true
console.log(document.images instanceof HTMLCollection); // true
console.log(document.forms instanceof HTMLCollection); // true
console.log(document.anchors instanceof HTMLCollection); // true
console.log(document.scripts instanceof HTMLCollection); // true

  由于HTMLCollection实例可以用HTML元素的id或name属性引用,因此如果一个元素有id或name属性,就可以在上面这五个属性上引用

<form name="myForm">
<script>
console.log(document.myForm === document.forms.myForm); // true
</script>

文档写入方法

  将输出流写入到网页的能力有4个方法:write()、writeln()、open()、close()

write()和writeln()

  write()和writeln()方法都接收一个字符串参数,即要写入到输出流中的文本

  write()会原样写入,而writeln()则在字符串的末尾添加一个换行符(\n),但换行符会被页面解析为空格

  在页面被加载的过程中,可以使用这两个方法向页面中动态地加入内容

<button id="btn">替换内容</button>
<script>
btn.onclick = function(){
document.write('123');
document.writeln('abc');
document.write('456');
}
</script>

open()和close()

  open()和close()方法分别用于打开和关闭网页的输出流

  open()方法实际上等于清除当前文档

<button id="btn">清除内容</button>
<script>
btn.onclick = function(){
document.open();
}
</script>

  close()方法用于关闭open方法所新建的文档。一旦关闭,write方法就无法写入内容了。如果再调用write方法,就等同于又调用open方法,新建一个文档,再写入内容。所以,实际上,close()只是和open()方法配套使用而已

<button id="btn">替换内容</button>
<script>
//相当于'123'又把'1'覆盖了
btn.onclick = function(){
document.open();
document.write('1');
document.close();
document.write('123');
}
</script>

  一般地,先使用open()方法用于新建一个文档,然后使用write()和writeln()方法写入文档,最后使用close()方法,停止写入

<button id="btn">替换内容</button>
<script>
btn.onclick = function(){
document.open();
document.writeln('hello');
document.write('world');
document.close();
}
</script>

  [注意]如果是在页面加载期间使用write()和writeln()方法,则不需要用到这两个方法

<button id="btn">内容</button>
<script>
document.writeln('hello');
document.write('world');
</script>

最后

  节点类型系列终于完结了

  DOM共有12种节点类型。其中,常用的有Element元素节点、Attribute特性节点、Text文本节点、Comment注释节点、Document文档节点和DocumentFragment文档片段节点

  欢迎交流

深入理解DOM节点类型第七篇——文档节点DOCUMENT的更多相关文章

  1. 深入理解DOM节点类型第四篇——文档片段节点DocumentFragment

    × 目录 [1]特征 [2]作用 前面的话 在所有节点类型中,只有文档片段节点DocumentFragment在文档中没有对应的标记.DOM规定文档片段(document fragment)是一种“轻 ...

  2. Java获取XML节点总结之读取XML文档节点

    dom4j是Java的XML API,用来读写XML文件的.目前有很多场景中使用dom4j来读写xml的.要使用dom4j开发,需要下载导入dom4j相应的jar文件.官网下载:http://www. ...

  3. 深入理解DOM事件类型系列第一篇——鼠标事件

    × 目录 [1]类型 [2]顺序 [3]坐标位置[4]修改键[5]相关元素[6]鼠标按键[7]滚轮事件[8]移动设备 前面的话 鼠标事件是web开发中最常用的一类事件,毕竟鼠标是最主要的定位设备.本文 ...

  4. 深入理解DOM事件类型系列第二篇——键盘事件

    × 目录 [1]类型 [2]顺序 [3]按键信息[4]应用 前面的话 鼠标和键盘是电脑端主要的输入设备,上篇介绍了鼠标事件,本文将详细介绍键盘事件 类型 键盘事件用来描述键盘行为,主要有keydown ...

  5. 深入理解DOM节点类型第五篇——元素节点Element

    × 目录 [1]特征 [2]子节点 [3]特性操作[4]attributes 前面的话 元素节点Element非常常用,是DOM文档树的主要节点:元素节点是html标签元素的DOM化结果.元素节点主要 ...

  6. 将HTML字符转换为DOM节点并动态添加到文档中

    将HTML字符转换为DOM节点并动态添加到文档中 将字符串动态转换为DOM节点,在开发中经常遇到,尤其在模板引擎中更是不可或缺的技术. 字符串转换为DOM节点本身并不难,本篇文章主要涉及两个主题: 1 ...

  7. javascript文档节点

    创建文本节点 document.createTextNode() 创建新文本节点,该方法接收一个参数,即要插入节点中的文本信息. <script> //创建一个div节点 var elem ...

  8. jQuery文档节点处理,克隆,each循环,动画效果,插件

    文档节点处理 //创建一个标签对象 $("<p>") //内部插入 $("").append(content|fn) ----->$(&quo ...

  9. java中XML操作:xml与string互转、读取XML文档节点及对XML节点增删改查

    一.XML和String互转: 使用dom4j程式变得很简单 //字符串转XML String xmlStr = \"......\"; Document document = D ...

随机推荐

  1. linux菜鸟日记(5)

    iptables详细语法及配置: SNAT:源地址转换DNAT:目标地址转换PNAT:端口地址转换 ----------------------------------iptables规则链 路由以后 ...

  2. .net工具

    程序名称 作者 说明 文件结构与元数据查看看 AssemblyView1.0   可以查看.net平台下exe,dll源代码的类结构,比如变量,属性,函数,事件的定义. Anakrino   源代码开 ...

  3. VMware创建Linux虚拟机并安装CentOS(三)

    选择“创建自定义布局”手动给Linux指定系统分区.交换分区,鼠标单击“下一步”按钮继续. 首先创建交Swap分区,鼠标单击“创建”按钮,在弹出的“生成存储”对话框中,生成分区选择“标准分区”:鼠标单 ...

  4. java.net.ConnectException: Connection timed out

    原因可能如下: (1)生成的WSDL的IP地址是不是服务器的IP: (2)代码中是否需要代理,若否,取消代理,若是,设置代理:

  5. linux下Vim的使用

    在vim中移动光标跟其他的编辑器中有很大的区别,不过一旦学会了,就会飞速的在文本中移动 复制粘贴dd 删除光标所在行dw 删除一个字(word)x 删除当前字符X 删除前一个字符D 删除到行末yy 复 ...

  6. Python的字节编译

    1.什么是Python的.pyc文件 在python中 .pyc文件是指以.pyc为后缀名的这一类文件,在我们的python的安装目录里,找到模块所在的目录Lib会看到很多以.py结尾的模块文件,与之 ...

  7. angularjs内置指令 - form

    form类 angular js对form表单进行了那些扩展 ①html原生form表单不允许嵌套,而angular封装之后的form可以进行嵌套 ②angular为form扩展了自动校验,和防止重复 ...

  8. 谢欣伦 - OpenDev原创教程 - 媒体开发库libMedia

    libMedia是一个免费的简单的媒体开发库,其中的接口类与函数大都以小写的x打头,来源于我的姓氏首字母(谢欣伦). 下载 OpenDev for VS2012 libMedia提供四大功能,一是视频 ...

  9. Thrift-0.9.2编译安装

    确定安装好了boost1.54以上 确定libevent版本大于1.0 只编译生成cpp库 ./configure --without-java --without-lua --without-pyt ...

  10. 基于Picture Library创建的图片文档库中的上传多个文件功能(upload multiple files)报错怎么解决?

    复现过程 首先,我创建了一个基于Picture Library的图片文档库,名字是 Pic Lib 创建完毕后,我点击它的Upload 下拉菜单,点击Upload Picture按钮 在弹出的对话框中 ...