Home >
Web Front-end >
JS Tutorial >
Javascript & DHTML Example Programming (Tutorial) DOM Basics and Basic API_Basic Knowledge
Javascript & DHTML Example Programming (Tutorial) DOM Basics and Basic API_Basic Knowledge
WBOY
Release: 2016-05-16 19:12:48
Original
1308 people have browsed it
1. What is DOM? What is DOM? DOM is the Document Object Model, which is a set of API interfaces based on browser programming (in this tutorial, it can be said to be DHTML programming). It is a recommended standard issued by the W3C. Each browser There are some subtle differences, among which Mozilla's browser is the closest to the standard. Simple Javascript needs to be combined with DOM to create beautiful effects and apply it to the WEB. This is almost the same as other languages. Just like C/C requires library support, otherwise it is just a matter of studying syntax. Therefore, you must have a certain understanding of DOM before you can apply Javascript to WEB or your RIA application, because DHTML In essence, it is to operate the DOM tree. I hope you can bring the DHTML.chm manual with you in future programming. If you need to be compatible with gecko, I also want to bring the gecko DOM manual because there are too many APIs. If you don’t know the interface, you can also check this manual
If you want to test whether the browser supports DOM, you can judge with a simple sentence
2. DOM tree Note: The root of the DOM tree is unified as the document root—document. Since the DOM is a tree structure, they naturally have the following relationships:
Root node (document)
Parent Node (parentNode)
Child Nodes (childNodes)
Brother Node Brother Node (sibling) (sibling)
Example:
Suppose the HTML of the web page is as follows
never-online's website
tutorial of DHTML and javascript programming
We refer to the concept of tree and draw the DOM tree of the HTML document structure:
html
head
div title
text text
from above You can see from the icon that html has Two child nodes, and html is the parent node of these two child nodes
Head has the node title, and there is a text node under the title
There is the node div under doby, and there is a text under the div Node
3. Manipulating the DOM tree As mentioned at the beginning, the essence of DHTML is to manipulate the DOM tree. How to operate it? Suppose I want to change the text of the div node in the above HTML document, how to do it? [code]
never-online's website
<script> <BR>var isSupportDOM = !!document.getElementById; //两个取反,这已经在上节中说过了,意思是强制转型 <BR>alert("你的浏览器 " +(isSupportDOM?"":"不")+ "支持 DOM!"); <BR></script>tutorial of DHTML and javascript programming<script> <BR> function changedivText (strText) { <BR> var nodeRoot = document; //这个是根结点 <BR> var nodeHTML = nodeRoot.childNodes[0]; //这个是html结点 <BR> var nodeBody = nodeHTML.childNodes[1]; //body结点 <BR> var nodeDiv = nodeBody.childNodes[0]; //DIV结点 <BR> var nodeText = nodeDiv.childNodes[0];//文本结点' <BR> nodeText.data = strText; //文本节点有data这个属性,因此我们可以改变这个属性,也就成功的操作了DOM树中的一个结点了 <BR> } <BR> </script>
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn