Home Web Front-end JS Tutorial JavaScript application analysis for DOM (3)_DOM

JavaScript application analysis for DOM (3)_DOM

May 16, 2016 pm 05:54 PM
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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the reason why ref binding to dom or component fails in vue3 and how to solve it What is the reason why ref binding to dom or component fails in vue3 and how to solve it May 12, 2023 pm 01:28 PM

Vue3ref binding DOM or component failure reason analysis scenario description In Vue3, it is often used to use ref to bind components or DOM elements. Many times, ref is clearly used to bind related components, but ref binding often fails. Examples of ref binding failure situations The vast majority of cases where ref binding fails is that when the ref is bound to the component, the component has not yet been rendered, so the binding fails. Or the component is not rendered at the beginning and the ref is not bound. When the component starts to render, the ref also starts to be bound, but the binding between ref and the component is not completed. At this time, problems will occur when using component-related methods. The component bound to ref uses v-if, or its parent component uses v-if to cause the page to

What does vue dom mean? What does vue dom mean? Dec 20, 2022 pm 08:41 PM

DOM is a document object model and an interface for HTML programming. Elements in the page are manipulated through DOM. The DOM is an in-memory object representation of an HTML document, and it provides a way to interact with web pages using JavaScript. The DOM is a hierarchy (or tree) of nodes with the document node as the root.

What are the ways to obtain DOM nodes in Vue3 What are the ways to obtain DOM nodes in Vue3 May 11, 2023 pm 04:55 PM

1. Native js gets the DOM node: document.querySelector (selector) document.getElementById (id selector) document.getElementsByClassName (class selector).... 2. Get the instance object of the current component in vue2: because each vue Each component instance contains a $refs object, which stores references to the corresponding DOM elements or components. So by default, the component's $refs point to an empty object. You can first add ref="name" to the component, and then pass this.$refs.

DOM manipulation guide in PHP DOM manipulation guide in PHP May 21, 2023 pm 04:01 PM

In web development, DOM (DocumentObjectModel) is a very important concept. It allows developers to easily modify and operate the HTML or XML document of a web page, such as adding, deleting, modifying elements, etc. The built-in DOM operation library in PHP also provides developers with rich functions. This article will introduce the DOM operation guide in PHP, hoping to help everyone. The basic concept of DOM DOM is a cross-platform, language-independent API that can

What are the dom and bom objects? What are the dom and bom objects? Nov 13, 2023 am 10:52 AM

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

What is the difference between bom and dom What is the difference between bom and dom Nov 13, 2023 pm 03:23 PM

BOM and DOM are different in terms of role and function, relationship with JavaScript, interdependence, compatibility of different browsers, and security considerations. Detailed introduction: 1. Role and function. The main function of BOM is to operate the browser window. It provides direct access and control of the browser window. The main function of DOM is to convert the web document into an object tree, allowing developers to Use this object tree to obtain and modify the elements and content of the web page; 2. Relationship with JavaScript, etc.

What are the built-in objects of DOM? What are the built-in objects of DOM? Dec 19, 2023 pm 03:45 PM

dom内置对象有:1、document;2、window;3、navigator;4、location;5、history;6、screen;7、document.documentElement;8、document.body;9、document.head;10、document.title;11、document.cookie。

What do dom and bom achieve? What do dom and bom achieve? Nov 20, 2023 pm 02:28 PM

DOM enables dynamic access and updates to web page content, while BOM provides APIs for interacting with browser windows, including controlling browser behavior and obtaining information about the browser and user environment. DOM is mainly used to operate web page content, while BOM is mainly used to operate browser windows and interact with browsers. The two together form an important foundation in Web front-end development, providing developers with rich methods to control and operate web pages and browsers to achieve strong interactivity, Web applications with good user experience.

See all articles