What is the difference between HTML attributes and DOM attributes?
What is the difference between HTML attributes and DOM attributes? This article will introduce to you the difference between HTML attributes and DOM attributes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Distinguish between DOM attributes and HTML element attributes
The attributes of HTML elements everyone You should all know, such as the src, id, etc. of the element.
Finally, the browser will parse HTML and build a DOM model, which means that the browser will parse HTML elements into DOM elements.
What is obtained in javascript are DOM elements, not HTML elements.
Most of the names and values of HTML element attributes and DOM attributes are the same, so many people mistakenly think that they are the same.
Distinguishing HTML element attributes and DOM attributes is a test of experience and memory. That was once. Now jQuery's attribute manipulation function attr() can forget these differences.
Use javascript to operate DOM attributes
Using javascript to operate DOM attributes is to operate the attributes of javascript objects. The properties of JavaScript objects do not need to be declared. There are many ways to access properties, as follows:
myImg.src = "xxxxxx" //使用“.”运算符 myImg["src"] = "xxxxxx" // 使用属性访问器 var propName = "src"; myImg[propName] = "xxxxxx" //属性访问器支持变量
Because the property accessor is provided, you can traverse all properties of a DOM object in the following way:
var result = ""; for (var p in myImg) { result += "属性名:" + p + ",属性值:" + myImg[p] + "\n"; }
Note that events or functions It is also a property of the object. If an object is a DOM object, it has many attributes by default
Use javascript to operate HTML element attributes
Use getAttribute and setAttribute in javascript to operate HTML Element properties. For example:
alert(myImg.getAttribute("class")); myImg.setAttribute("class","myclass2");
By changing the HTML attribute class, the className attribute of the corresponding DOM element will be changed. But not all HTML elements have corresponding DOM attributes. For example, custom HTML element attributes cannot be converted into DOM attributes. Another example is the element attribute "className" which is quite special because className corresponds to the HTML class.
What is the difference between HTML attributes and DOM attributes?
For browser engines, there is no such thing as "HTML tags". Its essence is DOM node object. There is no such thing as an "HTML document". Its essence is a document tree composed of DOM node objects. The browser engine is the "big brother" that actually stores and renders DOM node objects. It’s just that we can’t directly operate the browser engine, so we are not familiar with this essence (in fact, we don’t need to be familiar with it, but we need to know it).
The DOM node object is unique, but there is more than one way to manipulate the data of the DOM node object. For example, for the width of an image:
- ##HTML can be defined through the width attribute of
;
- JavaScript can be defined through element.width to read and modify;
- Don’t forget CSS, CSS can also be modified through the width attribute.
HTML attributes and JavaScript DOM object attributes are essentially just affecting DOM node object data One of many reasons.
Multiple reasons affect the actual data of the same DOM node (many to one), please be sure to remember this essential reason.
In detail:
HTML is just a description method for document trees and node objects.
- The
parser part of the browser directly passes the DOM document tree to the browser engine based on HTML.
- DOM objects can also be described using other methods, such as JSX. (Of course, when using other methods to describe DOM objects, the process of generating the DOM document tree will definitely be modified accordingly)
The DOM object in JavaScript is just a Interface for manipulating DOM objects in the browser engine.
- The DOM object in JavaScript and the DOM node stored in the browser engine are essentially not the same thing.
- Users actually only have the right to manipulate DOM objects provided in JavaScript.
- The JS engine and the browser engine cooperate to ensure that the JavaScript DOM object is an
original mapping of the DOM node in the engine.
- In this way, users can transparently modify the DOM nodes stored in the engine by operating JavaScript DOM objects.
- The browser engine is essentially responsible for re-rendering when the DOM tree is updated, and does not actually care about the existence of JS.
你如果用其他办法修改了引擎使用的DOM树,也能更新文档结构。(当然这种办法基本上不存在…)
至于HTML属性名和JavaScript DOM对象的属性名大多相似或等同,这仅仅是人为的方便。我如果喜欢我也可以设计成这样嘛:
// <img src="http://localhost/1.png" alt="alt text" width=640 height=480 /> node.DataSource = "http://localhost/1.png"; node.AlternativeText = "alt text"; node.Dimension.Width = 640; node.Dimension.Height = 480;
虽然这样就真的没法记了。
JavaScript DOM对象属性名和HTML属性名的近似,是JavaScript给Web开发者的恩惠。选择只记忆HTML属性名,然后记忆(或者是踩坑了再反查)JavaScript属性名中少量和HTML不同名的差异点,这是很自然的。
总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问JavaScript视频教程,jQuery视频教程,bootstrap教程!
The above is the detailed content of What is the difference between HTML attributes and DOM attributes?. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese

How to use JS and Baidu Maps to implement map polygon drawing function. In modern web development, map applications have become one of the common functions. Drawing polygons on the map can help us mark specific areas for users to view and analyze. This article will introduce how to use JS and Baidu Map API to implement map polygon drawing function, and provide specific code examples. First, we need to introduce Baidu Map API. You can use the following code to import the JavaScript of Baidu Map API in an HTML file
