document文档节点

概述:

document节点对象是文档的根节点,每张网页都有自己的document对象

document节点的属性

document.doctype获取octype节点
var doctype=document.doctype;
doctype //
document.name //“html”
如果该网站没有声明文档类型。则返回null
示例:
image.png
document.documentElement属性返回当前文档的根节点。

一般是html

docment.body/document.head返回文档中的body/head节点。

这两个属性总是存在的,如果省略了或,浏览器会自动创建

document.links属性返回当前文档所有设定了href属性的节点。
//打印文档所有的链接
var links =document.links
for(var i=0;i
示例:
image.png
结果:
image.png
document.forms返回当前文档所有的form表单节点
 /* HTML代码如下:*/
 
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()创建元素
示例:
image.png
结果:
image.png
document.createTextNode()创建文本节点
示例:
image.png
结果:
image.png
document.creareComment()创建注释节点
示例:
image.png
document.creaateAttribute()创建属性节点
 var a=document.createAttibute(“attr”)
  a.value=“newVal”
  node.setAttributeNode(a)
 //或者
 node.setAttribute(“attr”,“newVal”)
示例:
方法一:
image.png

image.png
方法二:
image.png

image.png

你可能感兴趣的:(document文档节点)