Home Web Front-end JS Tutorial Js process of operating DOM objects

Js process of operating DOM objects

Nov 28, 2017 pm 03:17 PM
javascript object process

Let me summarize the process of Js operation DOM object. I took a look at the speed of various methods of selecting elements. The native method is nearly 8 times faster than the jQUERY block, and IE8 is the slowest. , IE9 is three times faster than IE8.

Method to select document elements:

1. Select elements by ID (getElementById)
1) Usage method: document. getElementById("domId")
Where, domId is the id attribute value of the element to be selected
2) Compatibility: IE browsers that are lower than IE8 version do not distinguish the implementation of the getElementById method The element ID number is case-sensitive, and elements matching the name attribute will be returned.

2. Select elements by name (getElementsByName)
1) Usage method: document.getElementsByName("domName")
Among them, domName is the name attribute value of the element to be selected
2 ) Description: a. The return value is a nodeList collection (different from Array)
                  b. Unlike the ID attribute, the name attribute is only valid in a few DOM elements (form form, form element, iframe, img). This is because the name attribute was created to facilitate submitting form data.                                                                                                                                                                                                                                                                                                       ​ Therefore, the corresponding dom object can be referenced through document.domName 3) Compatibility: Elements with matching ID attribute values ​​in IE will also be returned together

3. Select elements by tag name (
getElementsByTagName
1) Usage method: element.getElementsByTagName("tagName") Among them, element is a valid DOM element (including document)
tagName is the tag name of the DOM element
2) Description : a. The return value is a nodeList collection (different from Array)
                b. This method can only select descendant elements of the element that calls this method.
C. Tagname does not distinguish between la between? For example: the images, forms, and links attributes of document point to the collection of ,

, tag elements, while document.body and document.head always point to the body and head tags (when the head declaration is not shown tag, the browser will also create the document.head attribute)

4. Select elements through CSS classes (getElementsByClassName)
1) Usage method: element.getElementsByClassName("classNames")
Among them, element is a valid DOM element (including document)
            classNames is a combination of CSS class names (multiple class names are separated by spaces, and can be separated by multiple spaces),
                                                                                                                                                                         ") will select elements whose descendants of elements have both class1 and class2 styles applied (the style names are in no particular order)
2) Description: a. The return value is a nodeList collection (different from Array)
b. This method can only select descendant elements of the element that calls this method.
3) Compatibility: IE8 and below browsers do not implement getElementsByClass
Name method


5. Select elements through
CSS selector
1) Usage method: document.querySelectorAll("selector")
Wherein, selector is a legal CSS selector 2) Description: a. The return value is a nodeList collection (different from Array) 3) Compatibility: IE8 and below browsers only support CSS2 standard selector syntax



I believe you have mastered the method after reading these cases. Please pay attention for more exciting information. Other related articles on php Chinese website!


Related reading:

How to use CSS to hide the text content of the image background

How to use Vue+CSS3 to create interactive effects

How to make DIV adaptive in height

The above is the detailed content of Js process of operating DOM objects. 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 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

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 open multiple Toutiao accounts? What is the process for applying for a Toutiao account? How to open multiple Toutiao accounts? What is the process for applying for a Toutiao account? Mar 22, 2024 am 11:00 AM

With the popularity of mobile Internet, Toutiao has become one of the most popular news information platforms in my country. Many users hope to have multiple accounts on the Toutiao platform to meet different needs. So, how to open multiple Toutiao accounts? This article will introduce in detail the method and application process of opening multiple Toutiao accounts. 1. How to open multiple Toutiao accounts? The method of opening multiple Toutiao accounts is as follows: On the Toutiao platform, users can register accounts through different mobile phone numbers. Each mobile phone number can only register one Toutiao account, which means that users can use multiple mobile phone numbers to register multiple accounts. 2. Email registration: Use different email addresses to register a Toutiao account. Similar to mobile phone number registration, each email address can also register a Toutiao account. 3. Log in with third-party account

Are Douyin sleep anchors profitable? What are the specific procedures for sleep live streaming? Are Douyin sleep anchors profitable? What are the specific procedures for sleep live streaming? Mar 21, 2024 pm 04:41 PM

In today's fast-paced society, sleep quality problems are plaguing more and more people. In order to improve users' sleep quality, a group of special sleep anchors appeared on the Douyin platform. They interact with users through live broadcasts, share sleep tips, and provide relaxing music and sounds to help viewers fall asleep peacefully. So, are these sleep anchors profitable? This article will focus on this issue. 1. Are Douyin sleep anchors profitable? Douyin sleep anchors can indeed earn certain profits. First, they can receive gifts and transfers through the tipping function in the live broadcast room, and these benefits depend on their number of fans and audience satisfaction. Secondly, the Douyin platform will give the anchor a certain share based on the number of views, likes, shares and other data of the live broadcast. Some sleep anchors will also

How to convert MySQL query result array to object? How to convert MySQL query result array to object? Apr 29, 2024 pm 01:09 PM

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

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

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

What is the difference between arrays and objects in PHP? What is the difference between arrays and objects in PHP? Apr 29, 2024 pm 02:39 PM

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values ​​are passed and object references are passed.

What should I pay attention to when a C++ function returns an object? What should I pay attention to when a C++ function returns an object? Apr 19, 2024 pm 12:15 PM

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

What is the Request object in PHP? What is the Request object in PHP? Feb 27, 2024 pm 09:06 PM

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

How do PHP functions return objects? How do PHP functions return objects? Apr 10, 2024 pm 03:18 PM

PHP functions can encapsulate data into a custom structure by returning an object using a return statement followed by an object instance. Syntax: functionget_object():object{}. This allows creating objects with custom properties and methods and processing data in the form of objects.

See all articles