查找 DOM 节点索引 [英] Finding DOM node index

查看:23
本文介绍了查找 DOM 节点索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到给定 DOM 节点的索引.就像做相反的事情

I want find the index of a given DOM node. It's like the inverse of doing

document.getElementById('id_of_element').childNodes[K]

鉴于我已经拥有对子节点和父节点的引用,我想改为提取 K 的值.我该怎么做呢?

I want to instead extract the value of K given that I already have the reference to the child node and the parent node. How do I do this?

推荐答案

在所有版本的 Safari、FireFox、Chrome 和 IE >= 9 中,没有任何框架的最短方式:

The shortest possible way, without any frameworks, in all versions of Safari, FireFox, Chrome and IE >= 9:

var i = Array.prototype.indexOf.call(e.childNodes, someChildEl);

这篇关于查找 DOM 节点索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆