document.defaultView:
一般情況下,在google瀏覽器開發者工具的console面板下運行document.defaultView === window
,這個是會返回true的。首先,我們得知道defaultView這個屬性到底是什麼意思呢?查下MDN:在瀏覽器中,返回與當前document所關聯的window對象,如果沒有則返回null
,而且是一個只讀屬性。前面說的一般情況下,是沒問題是因爲在Firefox瀏覽器低版本中會出問題,必須使用document.defaultView.getComputedStyle
來獲取其style
屬性。
children與childNodes的區別
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="parentDiv">
<!--註釋-->
<div></div>
</div>
<script type="text/javascript">
console.log(document.getElementById('parentDiv').childNodes); //NodeList[5]
console.log(document.getElementById('parentDiv').children) ; //HTMLCollection[1]
</script>
</body>
</html>
childNodes返回的是NodeList數組,是子節點的屬性,而children是返回子元素,返回的是元素的屬性。
Element是繼承與Node類型的,Node的NodeType具有多選值,而Element只是其中的一種。