10.1.2 Document 类型

1.基本信息

nodeType 9
nodeName #document
nodeValue null
parentNode null
ownerDocument null
childNodes 可能是DocumentType(最多一个)Element(最多一个)ProcessingInstruction或Comment

获取HTML元素

document.documentElement

获取body元素

document.body

获取文档类型

document.doctype //不常用

2.属性

document.title //标题
document.URL //当前页面地址
document.domain //当前页面域名
document.referrer //链接到当前页面的页面地址,没有时为空字符串''

在这里插入图片描述
后三者中,只有domain可以进行更改
eg.来自不同子域的页面想要通信,可以设置domain为相同值
‘p2p.wrox.com’与’www.wrox.com’
将二者均设置为

document.domain = 'wrox.com';

但是设置之后不可再设置回更加详细的域名地址

3.查找元素

document.getElementById 进行查找时,若文档中存在多个相同ID的元素,则只返回此ID第一次出现的元素
document.getElementsByTagName 进行查找时,返回一个HTMLCollection对象,其进行项的访问时,有四种方法

<img src='fir.gif'  name='theFir' />
<img src='sec.gif'  name='theSec' />
<img src='thi.gif'  name='theThi' />

想要访问第二个图片时

var images = document.getElementsByTagName('img');
//HTMLCollection对象
iamges['1'] 
images.item(1)
images['theSec']
images.namedItem('theSec')

获取所有元素

document.getElementsByTagName('*');

4.特殊集合

document.anchors //所有带name的a元素
document.forms //所有form元素
document.images //所有img元素
document.links //所有带href的a元素

5.文档写入

document.write('参数为一个字符串');
doucment.writeln('参数同样为字符串但是输出时会在末尾添加换行符');
document.open(); //打开网页的输出流
document.close(); //关闭网页的输出流
发布了43 篇原创文章 · 获赞 0 · 访问量 278

猜你喜欢

转载自blog.csdn.net/weixin_44774877/article/details/103969765
今日推荐