当前位置:网站首页>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边栏推荐
- Gorilla/mux framework (RK boot): add swagger UI
- New uniapp+uniui background management uniuadmin
- Deep scan log4j2 vulnerability using codesec code audit platform
- February 4, 2022: combined total IV. Give you a number composed of different integers
- Vs code remote SSH configuration
- Troubleshooting and solution of 4K video cannot be played on easydss live video on demand platform
- Detailed explanation of online reputation management
- There is no WM data in the primary commodity master data store view of SAP retail
- Automatically update site statistics with actions
- Source code analysis | activity setcontentview I don't flash
猜你喜欢

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030

Vulnhub DC-5

8. greed

6. template for integer and real number dichotomy

How to store, manage and view family photos in an orderly manner?

C language series - Section 4 - arrays

Spark broadcast variables and accumulators (cases attached)
What is sitelock? What is the function?

5. concept of ruler method

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027
随机推荐
Reading redis source code (VI) multi threading of redis 6.0
Delta oscillation in EEG
Learning records - things inherited by subclass parent of C #
The difference between script in head and body
Weekly Postgres world news 2022w04
Exploit format string vulnerability in CDE
Tencent cloud server CVM system tool configuration
An article shows you the difference between high fidelity and low fidelity prototypes
Section 6: basic configuration I of spingboot
[target tracking] open source | polytrack: use boundary polygons to quickly track and segment multiple targets, instead of bounding box and mask tracking
Function recursion and iteration
Transformation solution of digital intelligent supply chain platform for project management in engineering industry
Qingdao stadium has made headlines again, but it has nothing to do with sports
Pnas: amygdala individual specific functional connectivity: Fundamentals of precision psychiatry
5. concept of ruler method
2D visual empowerment smart water green intensive development
Reinforcement learning series (IV) -policygradient example
Autowired usage
Troubleshooting and optimization of easynvr version 5.0 Video Square snapshot not displayed
Integrated solution for intelligent supply chain platform management in rubber industry