当前位置:网站首页>How does native JS get the child elements of the parent element that the current element belongs to
How does native JS get the child elements of the parent element that the current element belongs to
2022-06-23 02:57:00 【It workers】
Include text and comment nodes
Native JS There is a common trick that is through the elements of previousSibling attribute , An additional note is that this property will traverse text node , Enter key .
From scratch, we can obtain the number of child elements of an element that belong to the parent element through such code .
var child = this;
while( (child = child.previousSibling) != null )
i++;The case code is as follows :
Html:
<ul id="ul"><li>123</li><li id="a">a</li><li>b</li><li>c</li></ul>
JS:
var child = document.getElementById("a");
var i = 0;
while((child = child.previousSibling) != null) i++;
console.log(i) //console 1 Through the loop traversal previousSibling Whether the property is null This little trick , You can get the position of the current element in the parent element .
Does not contain text nodes and comment nodes
The implementation code is as follows :
var child = document.getElementById("a");
var parent = child.parentNode;
var index = Array.prototype.indexOf.call(parent,child);
console.log(index)//1边栏推荐
- Detailed explanation of label smoothing and implementation of pytorch tenorflow
- OVS port traffic statistics practice
- Canvas draw the clock
- Why can only a small number of condition type prices be maintained in me12 of SAP mm?
- 8. greed
- The difference between script in head and body
- Markdown - mark above / below symbol (typora, latex)
- 2022-01-30: minimum good base. For a given integer n, if K (k) of n
- Goframe framework (RK boot): Based on cloud native environment, distinguish configuration files (config)
- Essentials of fleet video playback and fleet videoplayer video playback components
猜你喜欢

8. greed

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

C language series - Section 4 - arrays

6. template for integer and real number dichotomy

Vulnhub DC-5

How to store, manage and view family photos in an orderly manner?
What is sitelock? What is the function?

5. concept of ruler method

Spark broadcast variables and accumulators (cases attached)

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
随机推荐
Ping your domain name to 127.0.0.1. The solution to DNS hijacking
Salesforce fileUpload (I) how to configure the file upload function
Im web demo invite end hang up error avoidance
5 trends brought to us by customers
How to batch generate matrix 25 codes
Vulnhub DC-5
Construction and exploration of vivo database and storage platform
February 6, 2022: Arithmetic Sequence Division II - subsequence. Give you an integer array n
Analysis of ThreadLocal
Goframe framework (RK boot): rapid configuration of server CORS
Pnas: amygdala individual specific functional connectivity: Fundamentals of precision psychiatry
Use Sakura FRP intranet penetration service to build your own website / game server
Hypervisor Necromancy; Recover kernel protector (1)
February 3, 2022: a group of people (two or more) want to meet at the same place
Summary of easy-to-use MySQL interview questions (Part 1)
Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
February 2, 2022: the closest binary search tree value II. Given a non empty two
Learning record -- memo on constructors and attributes in C
What is ISBN code and how to make it
Learning about urldns chains