Home Web Front-end JS Tutorial What is javascript node

What is javascript node

Mar 31, 2021 am 11:26 AM
javascript

Javascript nodes refer to all content in the page, including tags, attributes, and text. The methods for obtaining JavaScript element nodes are: 1. getElementById() method; 2. getElementsByClassName() method, etc.

What is javascript node

The operating environment of this article: Windows 7 system, javascript version 1.8.5, DELL G3 computer

The nodes in JavaScript are all in the page The content (label, attribute, text (text, newline, space, carriage return)), Node.

Our commonly used node labels:

  • Element node (label)

  • Text node

  • Attribute node (attribute in label)

Get

element of the node There are many ways to get nodes

  • Document.getElementById()

  • Document.getElementsByClassName()

  • Document.getElementsByTagName()

  • Document.querySelector()

  • Document.querySelectorAll()

Getting attribute nodes

  • Element.attributes Gets the set of all attributes on the element

  • Element.setAttribute("Attribute name", "Attribute value") Set attributes and attribute values ​​​​to elements

  • Element.getAttribute("Attribute name") Method to get attribute values

  • Element.removerAttribute("attribute") deletes the attribute

Text node

There is no way to get it, no meaning

Get the child of the element Node

Element.childNodes This attribute is compatible. Standard browsers will obtain text nodes, but lower version browsers will not. Therefore, it is recommended to use the children attribute to obtain a single child node.

Get the first child node:

标准下   元素.firstElementChild
非标准下   元素.firstChild
Copy after login

Compatible writing method

var list=document.getElementById("list")
var fist=listElementChild||list.fistChild
console.log(fist)
Copy after login

Get the last child node

Element.lastElementChild Element.lastChild

Getting a sibling node

Element.previousSibling Element.prevElementSibling

Getting the next sibling node

Element.nextSibling Element.nextElementSibling

Get the parent node

Element.parentNode has no compatibility

Element.parentNode.parentNode

Distinguish between offsetparent and parentNode

DOM2 Create node

1. Method to generate node document. createElement ("div")

2. Method of inserting nodes

Parent node.appendChild(new node)

Insert a new node after the child node of the parent node

3. Insert a new node at the specified position

(1) Parent element.insetBefore (new node, in front of whom) Insert the new node in front of the specified element

4. Delete element node parent element.removerChild()

[Recommended learning: js basic tutorial

The above is the detailed content of What is javascript node. For more information, please follow other related articles on the PHP Chinese website!

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

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to implement an online speech recognition system using WebSocket and JavaScript

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to implement an online reservation system using WebSocket and JavaScript

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

How to use JavaScript and WebSocket to implement a real-time online ordering system

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecasting system

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

Simple JavaScript Tutorial: How to Get HTTP Status Code

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

How to get HTTP status code in JavaScript the easy way

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

How to use insertBefore in javascript

See all articles