让前端飞Web前端之路前端

《JS原理、方法与实践》- DOM中的HTML标准

2020-07-25  本文已影响0人  张中华

虽然HTML中的接口数量非常多,但是结构很简单。首先是HTMLDocument和HTMLElement,这两个接口分别继承自Core标准中Document和Element,它们在原来的基础上添加了HTML特有的属性。然后是两个Collection: HTMLCollection和HTMLOptionsCollection。


HTML子标准的整体结构

HTMLCollection

HTMLCollection用于表示HTML中相同类型节点的集合。例如:所有的div,所有的img,所有的span等等。
HTMLCollection接口只有三个属性:

html

<div id='div1'>a</div>
<div id='div2' name='two'>b</div>

js

var divCollection = document.getElementsByTagName('div');
console.log(divCollection instanceof HTMLCollection);
console.log(divCollection.length);
console.log(divCollection.item(0).textContent);
console.log(divCollection.namedItem('two').textContent);
测试结果

HTMLOptionsCollection

HTMLOptionsCollection和HTMLCollection接口类似,专门用来保存Select标签中Option标签所对应的HTMLOptionElement类型节点的集合。它比HTMLCollection接口多一个setLength(length)方法,用来指定Option节点的个数,其他方面和HTMLCollection接口完全相同。

HTMLDocument

HTMLDocument继承自Core子标准中的Document接口,用于表示HTML中的文档。HTMLDocument在Document的基础上增加了5个方法属性,3个读写属性和8个只读属性。

方法属性
读写属性
只读属性

HTMLElement

HTMLElement继承自Core的Element接口,新增了5个属性:

上一篇 下一篇

猜你喜欢

热点阅读