search
HomeWeb Front-endJS TutorialJavascript-DOM summary

Javascript-DOM summary

Oct 20, 2016 pm 01:31 PM

DOM summary

1. The meaning of DOM

DOM is the abbreviation of Document Object Model. According to the W3C DOM specification, DOM is a browser-, platform-, and language-independent interface that allows you to access other standard components of the page. Nodes in

DOM:

* The entire document is a document node.

* And each HMTL tag is an element node (divElement).

* The text in the label is a text node (div).

* The attribute of the label is the attribute node (divAttribute).

 * Everything is a node

2. Find an element

1. Get a tag by id,

document.getElementById('id name');

2. Get it by type name Multiple tags

var allA = document.getElementsByClassName('a');

3. Get multiple tags through the name (a or form) attribute of the tag

document.getElementsByName('corresponding name') ;

4. Get multiple tags through the tag name

var allDiv = document.getElementsByTagName('DIV')

5. Get a certain tag through the selector (if there are multiple tags, it will be found The first one)

var aDiv = document.querySelector('div');

6, get multiple tags through the selector

document.querySelectorAll('selector name');

3.DOM Node-element

1. Get all the text including the label

alert (label name.outerHTML) such as li

2. You can see all attribute information about the node through dir

console.dir (label name) such as li

3.for in you can see all the properties and methods about the node

4. Get the previous or next element node of a node

alert(li1.previousElementSibling.innerText );

alert(li1.nextElementSibling.innerText);

5.. Get the previous or next element node of a node (maybe a blank text node)

alert(li1.previousSibling.nodeName) ;

alert(li1.nextSibling.nodeName);

6. Get the first child node in ul

alert(ul.firstChild);

Get the first child element in ul! ! ! ! Node

alert(ul.firstElementChild);

alert(ul.lastElementChild.innerText);

7. Create a new li node

var newLi = document.createElement('li');

newLi.innerText = 'JQuery';

newLi.style.color = 'red';

8.Append a child node to the end of ul

ul.appendChild(newLi);

9.Use a new Node replaces a previous child node

ul.replaceChild(newLi,li1);

10. Remove a child node (the node to be removed must be a child node of ul)

ul.removeChild (newLi.previousElementSibling);

11. Insert a new node to a certain child node

ul.insertBefore(newLi,li1);

12. Insert a new node object position into ul +Node object

'beforeBegin', 'afterBegin', 'beforeEnd', 'afterEnd'

ul.insertAdjacentElement('afterEnd',newLi);

13. Insert html code

ul.insertAdjacentHTML('beforeBegin ','

ppppp

');

14. Insert text

ul.insertAdjacentText('afterBegin','afterbegin')

4.DOM node-text

1.for traversal

for (var i = 0; i

Use camel case naming method to name variables or functions goShoppingToMall

var aNode = ulChild[i];

Determine whether the remembered point currently traversed is a certain Node type of a system Element ELEMENT Attribute ATTRIBUTE Text TEXT

if (aNode.nodeType == Node.ELEMENT_NODE) ​​{

Macro definition Use numbers to represent node types 1, element 2, attribute node 3, text node

alert (aNode.nodeType);

alert(aNode.nodeName);

}

}

2.children Get the internal children! ! ! element! ! ! Node

childNode gets the internal child nodes (including text nodes)

var cssText = ul.children[1].childNodes[0];

gets the text in the text node

alert(cssText.nodeValue);

alert(cssText.textContent);

3. Append data

cssText.appendData('CSS');

a: Starting from which character and starting from 0

b: How long to delete the data

cssText.deleteData(3,1);

4. Replace a certain range of characters with another range of characters

cssText.replaceData(1,2,'CCCCC');

5. Insert a certain range of characters into A certain position (consider the position after insertion)

cssText.insertData(2,'A');

5. Remove the text in the text node

cssText.remove();

5.DOM Node-Attributes

1. All attributes

alert(a.attributes.length);

2. Directly call the get method on the element node to obtain

alert(a.getAttribute('title'));

3. You can also modify the value of an attribute through the set method

a.setAttribute( 'title', 'Click me');

4. You can also quickly get the value of a certain attribute by typing

alert(a.title);

a.title = 'No more Clicked ';

5. Set the shortcut key

alt + shift + A Test in the browser

a.accessKey = 'A';

6. Set whether the label can be edited

a. contentEditable = 'true';

7. Determine whether the element contains an attribute

alert(a.hasAttribute('title'))

8. Get the type of element

alert(a.className)

9. Directly modifying the type of an element may cause the previous type to be lost

a.className = 'bigSize yellowText';

Directly adding a new type to the type list of a will not affect the previous type

a.classList.add('border');

Delete an attribute

a.classList.remove('bigSize');

10. Switch whether to use a certain type

If there is one, remove it If it doesn’t exist originally, add

a.classList.toggle('bigSize');

11. The style just set through js can be obtained

The style written directly in the attribute can be obtained

written in the style The style js in the table (Style tag) cannot be obtained

a.style.padding = '20px';

alert(a.style.padding);

12. After obtaining the calculation (including the attributes in the , the style in the style sheet, modified in js)

var aStyle = window.getComputedStyle(a,':after');

alert(aStyle.border);


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
From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software