search
HomeWeb Front-endJS TutorialJavaScript application analysis for DOM (3)_DOM

If this DOM element has no style, there will be no operation. 2. We can also directly use JS to dynamically write DOM elements into html.
Today in this chapter we will talk about these two applications
(1) Operate existing DOM elements in html.
As I said above, operating on existing DOM elements is nothing more than operating on styles. So we first need to be able to get the style of this DOM element. Before talking about getting the style of DOM elements. First let’s talk about the style linking method of DOM elements. There are three types.

One is to write styles directly in the html document such as

.

The second is to insert style tags in the head of the html document, such as
#dom{width:300px;height:200px;background:#000;}


The third is our commonly used linking methods, such as


These three methods of using JS to operate its style are not the same
The key point is that we talk about the third A link-in style operation, because it is the most commonly used and the most convenient.
The second linking style is troublesome to operate.
The third type of link style is troublesome to operate, and the style cannot be modified directly. If you want to modify it, you must use the first method, which means you can only see but not touch it

The first type of link How to operate the style
Example


To get its height attribute, the first step is to get the DOM element. Use the method in the previous chapters
var a = document.getElementById("dom ");
Get its height attribute again, it's very simple
var h = a.style.height;
And so on, get the width, get the background color
var w = a.style. width;
var bg = a.style.background;
Note that the margin attribute is margin-top;
To obtain this, you cannot write directly
var mt = a.style.margin-top;
It is necessary to use the camel writing method mentioned in JQ
var mt = a.style.marginTop;

Of course it is useless to obtain it. We need to be able to modify it, and it is very convenient to modify. For example, if we want to change its height to 100, it is very simple, just say
a.style.height = "100px";
The rest are the same, I won’t say more;


The second method of linking styles
This operation requires distinguishing between browsers. Because IE and FF have different codes for obtaining this, for example, the method of obtaining the height is
var domcss = document.styleSheets[0].cssRules||document.styleSheets[0].rules;
var a = domcss[ 0].style.height;
The modification is like this
domcss[0].style.height = "100px";
I don’t want to explain why it is written like this. If you are interested, check it out yourself;
The third method of linking style operation


This operation also needs to distinguish between browsers.
To obtain it, you usually write a function. The function is like this
function CurrentStyle(element){
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
Suppose there is a height attribute in our css.css file
The acquisition method is var a = CurrentStyle("dom").height;
It cannot be modified directly by the method here, and can only be modified by the first method.
I don’t want to explain why this is written like this. If you are interested, check it out yourself;

(2) Use JS to dynamically create DOM elements.
Actually, this is very simple, just a few JS methods, but it needs to be done step by step like building a house. For example, I want to create a DOM element like this:




The first step is to create a div node. var newobj = document.createElement("div");

The second step is to add an id attribute to this section, and the attribute name is dom. newobj.setAttribute("id","dom");

The third step is to add attributes to this node. There are two ways. One is to modify the style we mentioned earlier, like this newobj .style.width = "100px"; Another method is newobj.setAttribute("width", "100px") used in the second step, and so on for other attributes

The fourth step is To put this node into an html document, the method is like this: document.body.appendChild(newobj) means this. document.body obtains the body element
, and appendChild(newobj) adds a child element to the body element, which is the node we created.


If you want to remove this node, this is document.body.removeChild(newobj);
(This can be replaced with the one you want to add child elements to. Element, for example, if I want to add a node to the element with the id of con, we just write document.getElementById("con").appendChild(newobj))

This is complete. There are many methods similar to appendChild in JS. The usage is the same as this. If you are interested, you can go to Baidu. So I won’t talk about it here, as they are not commonly used. Okay, that’s it for this chapter. In the next chapter, I will teach you how to write some effects.
Statement
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
JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.