search
HomeWeb Front-endJS TutorialEfficiently obtain which child element the current element is of the parent element_javascript skills

For example, when processing events, sometimes you need to know which child node is currently clicked, but the HTML DOM itself does not directly provide the corresponding attributes, so you need to calculate it yourself.

From an index number, it is easy to get the child node or child element corresponding to the index. Just use parentNode.childNodes[index] or parentNode.children[index] directly.

But conversely, knowing a node or element object is not so straightforward to know its index number.

For some special elements, HTML DOM has corresponding attributes indicating their index numbers, mainly the TD and TR elements of tables.

Table cell TD element has cellIndex attribute.

The table row TR element has a rowIndex attribute.

If your processing target happens to be a table, use these two attributes first.

But general nodes or elements do not have attributes such as childNodeIndex or childElementIndex.

Solutions are mainly divided into two categories:

1. Pre-calculate and cache the index number of the node (can be stored in node attributes or js variables).

2. Real-time calculation requires traversing some nodes.

In application, one of the above two types of solutions can be selected according to different actual situations.

Scenarios where option 1 is applicable:

When the DOM structure will not change and the index of individual nodes needs to be obtained frequently, option 1 can be used.

The advantage is that subsequent reading is fast, but the disadvantage is that initialization requires overhead and needs to be re-initialized after the DOM structure changes.

Situations where option 2 is applicable:

The DOM structure may change, and the index of individual nodes is not obtained particularly frequently, option 2 can be used.

The advantage is that it is not affected by DOM structure changes, does not pollute the DOM structure, and has no initialization overhead. The disadvantage is that it is not suitable for high-frequency calls.

Generally speaking, it is better to adopt option 2, because usually the size of the DOM tree is relatively limited, and one cycle of loops will not significantly reduce the overall performance, and its advantages are significant.

For IE browser, there is a more direct method.

From IE4 to IE11, there is a sourceIndex attribute. This attribute represents the order of the elements in the DOM tree. Comparing the difference in the sourceIndex of the element and the parent element makes it easy to know which child element the element is.

I wrote a function to differentiate the processing. Under IE, sourceIndex is used for efficient judgment, while for non-IE, general traversal is used.

Copy code The code is as follows:

function getChildrenIndex(ele){
//IE is simplest and fastest
if(ele.sourceIndex){
return ele.sourceIndex - ele.parentNode.sourceIndex - 1;
}
//other browsers
var i=0;
while(ele = ele.previousElementSibling){
i ;
}
return i;
}

The above function just calculates the element Element, which is nodeType Nodes with a value of 1, text nodes, annotation nodes, etc. will not be counted. If you need to calculate all nodes, sourceIndex cannot be used, because this attribute is only for Element. previousElementSibling should also be changed to previousSibling accordingly. Then it must be written as the following function:
Copy code The code is as follows:

function getNodeIndex(node){
var i=0;
while(ele = ele.previousSibling ){
i; The performance of testing this method is very poor. Its internal implementation mechanism is definitely not like IE that caches resource index numbers. If this method is extremely efficient, it can be calculated using the dichotomy method to improve efficiency, but it is not possible yet.


Final summary:

For table TD and TR elements, use the cellIndex and rowIndex attributes first.

For IE, the sourceIndex attribute is preferred. In other cases, use previousElementSibling or previousSibling to traverse.

The performance of the compareDocumentPosition method is very poor.
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
如何在jQuery中删除最后一个子元素?如何在jQuery中删除最后一个子元素?Feb 19, 2024 pm 09:40 PM

jQuery是一个流行的JavaScript库,用于简化Web开发中的许多任务,包括DOM操作。在网页开发中,经常需要对DOM元素进行增删改查的操作,其中删除最后一个子元素也是一个常见需求。本文将介绍使用jQuery删除最后一个子元素的几种方法。方法一:使用last()方法jQuery提供了last()方法,可以选取当前查询结果的最后一个元素。通过结合这个方

了解事件冒泡机制:为何子元素的点击会影响父元素的事件?了解事件冒泡机制:为何子元素的点击会影响父元素的事件?Jan 13, 2024 pm 02:55 PM

理解事件冒泡:为什么子元素的点击会触发父元素的事件?事件冒泡是指在一个嵌套的元素结构中,当子元素触发某个事件时,该事件会像冒泡一样逐层传递到父元素,直至最外层的父元素。这种机制使得子元素的事件可以在整个元素树中传递,并依次触发所有相关的元素。为了更好地理解事件冒泡,让我们来看一个具体的示例代码。HTML代码:

使用:nth-child(n+3)伪类选择器选择位置大于等于3的子元素的样式使用:nth-child(n+3)伪类选择器选择位置大于等于3的子元素的样式Nov 20, 2023 am 11:20 AM

使用:nth-child(n+3)伪类选择器选择位置大于等于3的子元素的样式,具体代码示例如下:HTML代码:<divid="container"><divclass="item">第一个子元素</div><divclass="item"&

使用jQuery删除元素的最后一个子元素使用jQuery删除元素的最后一个子元素Feb 26, 2024 pm 12:39 PM

如何使用jQuery删除最后一个子元素?在前端开发中,经常会遇到需要对页面元素进行增删改查的操作。其中,删除最后一个子元素是一个常见的需求。本文将介绍如何使用jQuery来删除最后一个子元素,并附上具体的代码示例。首先,我们需要在页面中引入jQuery库,确保能够使用其中的功能。在HTML文件中添加以下代码:<

使用:nth-child伪类选择器选择特定位置的子元素的CSS样式使用:nth-child伪类选择器选择特定位置的子元素的CSS样式Nov 20, 2023 pm 04:43 PM

使用:nth-child伪类选择器选择特定位置的子元素的CSS样式在CSS中,伪类选择器是用于选择HTML文档中特定状态的元素。除了常见的伪类选择器如:hover和:active,还有一个非常有用的伪类选择器是:nth-child,它允许我们选择特定位置的子元素。:nth-child伪类选择器的语法如下:父元素:nth-child(n)其中父元素代表父级元素

如何利用jQuery判断元素是否包含子元素如何利用jQuery判断元素是否包含子元素Feb 28, 2024 am 11:03 AM

如何利用jQuery判断元素是否包含子元素在网页开发中,经常会遇到需要判断一个元素是否包含子元素的情况。使用jQuery可以非常方便地实现这个功能。在本文中,将介绍如何利用jQuery判断一个元素是否包含子元素,并给出具体的代码示例。在jQuery中,可以使用children()方法来选择指定元素的所有直接子元素。如果一个元素包含子元素,那么使用childr

jQuery实现判断元素是否存在子元素功能jQuery实现判断元素是否存在子元素功能Feb 28, 2024 pm 12:54 PM

jQuery是一个广泛应用于前端开发的JavaScript库,它提供了简洁而强大的API,可以方便地操作DOM元素。在实际开发中,有时候我们需要判断一个元素是否包含子元素,这时就需要使用jQuery来实现。要判断一个元素是否存在子元素,我们可以利用jQuery提供的方法来实现。以下是一个示例代码,演示了如何使用jQuery来判断一个元素是否包含子元素:<

jQuery实例:如何利用jQuery删除最后一个子元素?jQuery实例:如何利用jQuery删除最后一个子元素?Feb 20, 2024 pm 07:45 PM

标题:jQuery实例:如何利用jQuery删除最后一个子元素?在Web开发中,经常会遇到需要通过JavaScript操作DOM元素的情况。而jQuery作为一个广泛使用的JavaScript库,提供了许多便捷的方法来操作DOM元素。本文将通过一个实例来介绍如何利用jQuery删除最后一个子元素的方法,并提供详细的代码示例。首先,我们需要在HTML文件中引入

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.