1、文档:DOM中的“D”
"D"代表document(文档)
2、对象:DOM中的“O”
“O”代表object(对象) 对象的分类
- 用户定义对象(user-defined object)
- 内建对象(native object)
- 宿主对象(host object)
window对象window对象对应着浏览器窗口本身,这个对象的属性和方法通常统称为BOM(浏览器对象模型)BOM提供了window.open和window.blur等方法。以至于被滥用于各种弹出窗口和下拉菜单
3、模型:DOM中的“M”
“M”代表“Model”(模型)DOM把一份文档表示为一棵树(数学意义上的概念)示例代码
<!DOCTYPE html><html lang="en"> <head> <meta charset="utf-8" /> <title>Shoping List<title> </head> <body> <h1 id="What-to-buy">What to buy</h1> <p title="a gentle reminder">Don’t forget to buy this stuff.<p> <ul id="purchases"> <li> A tin of beans<li> <li class="sale">Cheese<li> <li class="sale important">Milk<li> </ul> <body> </html>代码中<html>相当于树根,即根元素。<head>和<body>属于下一个分支,位于同一层次且互不包含,属于兄弟关系。<head>元素有两个子元素<meta>和<title>(属于兄弟关系)<body>元素有三个子元素<p>、<h1 id="ul-这三个属于兄弟关系-ul-也是一个父元素-有三个子元素-他们都是-li-元素-pre-p-如果你能把一个文档的各种元素想象成一棵家谱树-我们就可以用同样的术语描述DOM-但我觉得称为-strong-节点树-strong-更准确-p-h-节点">、<ul>(这三个属于兄弟关系。<ul>也是一个父元素,有三个子元素,他们都是<li>元素。
如果你能把一个文档的各种元素想象成一棵家谱树,我们就可以用同样的术语描述DOM。但我觉得称为“节点树”更准确
4、节点
节点(node)属于网络术语,它表示网络中的一个连接点。一个网络就是由一些节点构成的集合。DOM也是同样的情况,文档是由节点构成的集合。
- 元素节点
- 文本节点
- 属性节点
4、1元素节点
DOM的原子是元素节点(element node)诸如
、、
4、2文本节点
在上述例子中,
元素包含着文本“don't forget to buy this stuff.”它就是一个文本节点(text node)。
4、3属性节点
属性节点是对元素做出更具体的描述。例如,几乎所有的元素都有一个title属性,我们可以利用这个属性对包含在元素里的东西做出准确的描述:
<p title="a gentle reminder">Don't forget to buy this stuff.<p>
在DOM中title="a gentle reminder"是一个属性节点(attribute node),在前面的例子中无序清单元素
- 有个id属性。有些清单元素
- 有class属性。
三者之间的关系.png
4、4 CSS
类似javascript脚本,我们也可以将CSS样式嵌在文档
部分(style>标签之间)。也可以放在另外的一个文件里。**在HTML文件中引用CSS文件的格式:<link type="text/css" href="file.css" rel="stylesheet">
继承(inheritance)是CSS技术中的一项强大功能。1)、 class属性
<p class="special">This pargraph has the special class<p><h2 id="So-dose-this-headline">So dose this headline</h2>
在样式表里可以为上面的代码进行定义
special{font-style: italic;}
还可以这样定义
h2.special{text-transform: uppercase;}
2)、id属性id属性的用途是给网页里的某个元素加上一个独一无二的标识符:
<ul id="purchases">
样式表定义
#purchases{border:1px solid white;background-color:#333;color:#ccc;padding:1em;}
#purchases li{font-weight:bold;}
4、5获取元素
有3种DOM方法可获取元素节点,分别是通过元素ID、通过标签名和通过类名字来获取
- getElementById
- getElementsByTagName
- getElementsByClassName
1)getElementById
此方法将返回一个与那个有着给定id属性值的元素节点对应的对象,在javascript里注意大小写。它是document对象特有的函数,在脚本代码里,函数名的后面必须跟有一对圆括号,这对圆括号包含这函数的参数。document.getElementById(id)在getElementById方法中只有一个参数:你想获得的那个元素的id属性的值,这个id属性必须放在单引号或双引号里。docment.getElementById("purchases")这个调用将返回一个对象,这个对象对应着document对象里的一个独一无二的元素,那个元素的HTLM id属性值是purchases
Shoping List What to buy
Don’t forget to buy this stuff.
<ul id="purchases">
- A tin of beans
- Cheese
- Milk
验证可得是一个对象
2)getElementsByTagName
getElementsByTagName方法返回一个对象数组,每个对象分别对应着文档里有着给定标签的一个元素。它的参数是标签的名字:decument.getElementByTagName(tag)
alert(document.getElementsByTagName("li").length);//显示文档里的列表元素个数为:3
Shoping List What to buy
Don’t forget to buy this stuff.
<ul id="purchases">
getElementsByTagName允许把一个通配符作为它的参数,通配符(*)必须放在引号里
alert(document.getElementsByTagName("*").length);//可以知道文档里有多少个元素节点
var shopping=document.getElementById("purchases");var items=shopping.getElementsByTagName("*");//程序运行结果,items数组将只包含id属性值是purshase的元素清单的元素
3)getElementByClassName
这个方法让我们能够通过class属性中的类名来访问元素,getElementByClassName也只接受一个参数,就是类名:
getElementByClassName(class)
这个方法的返回值也与getElementsByTagName类似,都是一个具有相同类名的元素的数组。
document.getElementsByClassName("sale")
利用这种方法还可有查找那些带有多个类名的元素。多个类名之间用空格分开即可
alert(document.getElementsByClassName("important sale").length);//对话框显示1,表示只有一个元素匹配。类名的顺序不重要,就算元素还带有更多类名也没有关系。
也可以和getElementById组合使用
var shopping=document.getElementById("purchase"); var sales=shopping.getElementsByClassName("sale");sales数组中包含的就只是位于“purchases”列表中的带有“sale”类的元素。
getElementByClassName方法非常有用,但只有较新的浏览器才支持它。所以,需要使用已有的DOM方法来实现自己的getElementsByClassName。
function getElementsByClassName(node,classname){if (node.getElementsByClassName){//使用现有的方法return node.getElementsByTagName("*");for (var i=0; i<elems.length;i++){ if(elems[i].ClassName.indexof(classname)!= -1){results[results.length]=elems[i]; } }return results; }}
5 获取和设置属性
- getAttribute方法(获取元素的属性)
- setAttribute方法(更改属性节点值)5、1getAttributegetAttribute是一个函数,它只有一个参数(你所要查询的属性的名称)

HTMLtagsareessentialforstructuringwebpages,enhancingaccessibility,SEO,andperformance.1)Theyareenclosedinanglebracketsandusedinpairstocreateahierarchicalstructure.2)SemantictagslikeandimproveuserexperienceandSEO.3)Creativetagslikeenabledynamicgraphics

Self-closingtagsinHTMLandXMLaretagsthatclosethemselveswithoutneedingaseparateclosingtag,simplifyingmarkupstructureandenhancingcodingefficiency.1)TheyareessentialinXMLforelementswithoutcontent,ensuringwell-formeddocuments.2)InHTML5,usageisflexiblebutr

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
