English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
-----引入
每个载入浏览器的 HTML 文档都会成为 Document 对象。
Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问。
属性
1 document.anchors 返回对文档中所有 Anchor 对象的引用。还有document.links/document.forms/document.images等
2 document.URL 返回当前文档的url
3 document.title 返回当前文档的标题
4 document.body 返回body元素节点
方法
1 document.close() 关闭用 document.open() 方法打开的输出流,并显示选定的数据。
<!DOCTYPE html> <html> <head> <script> function createDoc() { var w=window.open(); w.document.open(); w.document.write("<h1>Hello World!</h1">); w.document.close(); } </script> </head> <body> <input type="button" value="New document in a new window" onclick="createDoc()"> </body> </html>
2 document.createElement() 创建元素节点。
<!DOCTYPE html> <html lang="ko"> <!DOCTYPE html> <head> </head> <body> <p>hello world</p> <script> var a = document.createElement('hr'); document.body.appendChild(a) </script> </body> </html>
3 document.createAttribute() 创建属性节点。
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to make a BUTTON element.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var btn=document.createElement("BUTTON"); document.body.appendChild(btn); } </script> </body> </html>
4 document.createTextNode()는 텍스트 노드를 생성합니다.
<!DOCTYPE html> <html lang="ko"> <!DOCTYPE html> <head> </head> <body> <p>hello world</p> <script> var a = document.createTextNode('hahahah'); document.body.appendChild(a) </script> </body> </html>
5 document.getElementsByClassName()는 문서에 모든 지정된 클래스 이름을 가진 요소 집합을 반환합니다. NodeList 객체 집합은 배열과 유사한 데이터 형식으로, length 속성만 제공하고 배열에서의 push, pop 메서드와 같은 메서드는 제공하지 않습니다.
6 document.getElementById()는 지정된 id를 가진 첫 번째 객체에 대한 참조를 반환합니다.
7 document.getElementsByName()는 지정된 이름을 가진 객체 집합을 반환합니다.
8 document.getElementsByTagName()는 지정된 태그 이름을 가진 객체 집합을 반환합니다.객체 집합.
9 document.write()는 문서에 HTML 표현식이나 JavaScript 코드를 작성하는 방법을 보여줍니다. 주의: html 문서가 로드된 후에 write 메서드를 사용하면 원래 html 문서가 write 내용으로 덮어지게 됩니다.
<!DOCTYPE html> <html lang="ko"> <!DOCTYPE html> <head> </head> <body> <p>hello world</p> <script> document.write('hahahh') </script> </body> </html>
이제 저는 여러분께 제공한 js 기본 DOM에서 document 객체의 일반 속성 및 메서드 설명에 대해 다루었습니다. 많은 지지와 지지를 부탁드립니다.