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
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.
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.

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.

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 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

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.

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.

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 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.

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.


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 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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

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 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.
