document文档节点
2018-08-08 本文已影响0人
木子木同
概述:
document节点对象是文档的根节点,每张网页都有自己的document对象
document节点的属性
document.doctype获取octype节点
var doctype=document.doctype;
doctype //<!DOCTYPE html>
document.name //“html”
如果该网站没有声明文档类型。则返回null
示例:

document.documentElement属性返回当前文档的根节点。
一般是html
docment.body/document.head返回文档中的body/head节点。
这两个属性总是存在的,如果省略了<head>或<body>,浏览器会自动创建
document.links属性返回当前文档所有设定了href属性的<a>及<area>节点。
//打印文档所有的链接
var links =document.links
for(var i=0;i<links.length;i++){
console.log(links[i])
}
示例:

结果:

document.forms返回当前文档所有的form表单节点
/* HTML代码如下:*/
<form name=“foo” id=“bar”></form>
document.forms[0]===document.forms.foo //true
document.forms.bar===document.forms.foo //true
document.images返回页面所有的img图片节点
document.title返回当前文档的标题,可读可写。
document.charset返回当前的编码方式 ,返回UTF-8
document的方法
document.createElement()创建元素
示例:

结果:

document.createTextNode()创建文本节点
示例:

结果:

document.creareComment()创建注释节点
示例:

document.creaateAttribute()创建属性节点
var a=document.createAttibute(“attr”)
a.value=“newVal”
node.setAttributeNode(a)
//或者
node.setAttribute(“attr”,“newVal”)
示例:
方法一:


方法二:

