《DOM启示录》第二章

第二章 document节点

2.1 document节点

document节点的构造器是HTMLDocuemnt,HTMLDocument的原型的构造器是Document:

<!DOCTYPE html>
<html lang="en">
<body>
<script>
console.log(window.document instanceof Document);
console.log(window.document.constructor); //logs function HTMLDocument() { [native code] }
console.log(window.document.constructor.prototype instanceof Document);
console.log(window.document.nodeType); //logs 9, which is a numeric key mapping to DOCUMENT_NODE

</script>
</body>
</html>

2.2 HTMLDocument的属性和方法

<!DOCTYPE html>
<html lang="en">
<body>
<script>

//document自身的属性
console.log(Object.keys(document).sort());

//document所有的属性
var documentPropertiesIncludeInherited = []; for(var p in document){ documentPropertiesIncludeInherited.push(p); } console.log(documentPropertiesIncludeInherited.sort()); //document继承的属性

var documentPropertiesOnlyInherited = []; for(var p in document){ if( !document.hasOwnProperty(p)){documentPropertiesOnlyInherited.push(p); } } console.log(documentPropertiesOnlyInherited.sort()); </script> </body> </html>

 

常见的属性:

doctype

  • documentElement
  • implementation.*
  • activeElement
  • body
  • head
  • title
  • lastModified
  • referrer
  • URL
  • defaultview
  • compatMode
  • ownerDocument
  • hasFocus()

 

2.3

<!DOCTYPE html>
<html lang="en">
<body>
<script>

var d = document;
console.log('title = ' +d.title);
console.log('url = ' +d.URL);
console.log('referrer = ' +d.referrer);
console.log('lastModified = ' +d.lastModified);

//logs either BackCompat (Quirks Mode) or CSS1Compat (Strict Mode)
console.log('compatibility mode = ' +d.compatMode);

</script>
</body>
</html>

2.4 document的两个属性

<!DOCTYPE html>
<html lang="en">
<body>
<script>

//This is the doctype/DTD
console.log(document.childNodes[0].nodeType); //logs 10, which is a numeric key mapping to DOCUMENT_TYPE_NODE

//This is the <html> element
console.log(document.childNodes[1].nodeType); //logs 1, which is a numeric key mapping to ELEMENT_TYPE_NODE

</script>
</body>
</html>

2.5 document提供了head,body,doctype的快捷方式

document.doctype refers to <!DOCTYPE> 

  • document.documentElement refers to <html lang="en">
  • document.head refers to <head>
  • document.body refers to <body>

2.6 使用document.implementation.hasFeature()检查DOM使用了DOM 2还是DOM3方案

<!DOCTYPE html>
<html lang="en">
<body>
<script>

console.log(document.implementation.hasFeature('Core','2.0'));
console.log(document.implementation.hasFeature('Core','3.0')); 

</script>
</body>
</html>

2.7 使用document.activeElement来获取当前激活的节点

<!DOCTYPE html>
<html lang="en">
<body>
<textarea></textarea>

<script>

//set focus to <textarea>
document.querySelector('textarea').focus();

​//get reference to element that is focused/active in the document
console.log(document.activeElement); //logs <textarea>

</script>
</body>
</html>

2.8 window.defaultView

<!DOCTYPE html>
<html lang="en">
<body>
<script>

console.log(document.defaultView) //reference, head JS object. Would be window object in a browser.

</script>
</body>
</html>

 

2.9 ownerElement

<!DOCTYPE html>
<html lang="en">
<body>

<iframe src="http://someFileServedFromServerOnSameDomain.html"></iframe>

<script>

//get the window.document that the <body> is contained within
console.log(document.body.ownerElement);

//get the window.document the <body> inside of the iframe is contained within
console.log(window.frames[0].document.body.ownerElement);

</script>
</body>
</html>

 

转载于:https://my.oschina.net/u/1792175/blog/598015

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值