javascript - 获取 div 中存在的元素的索引?
问题描述
<div id="div">
<h1 id="1">Index 1</h1>
<h1 id="2">Index 2</h1>
<h1 id="3">Index 3</h1>
</div>
// Something like this, but this doesn't work.
const div = document.getElementById('div').childNodes;
const _h1 = document.getElementById('2');
for (const i of div) {
if (i == _h1) {
console.log(div.indexOf(i);
}
}
我有一个div
有一些子节点的。我也有一个元素_h1
。_h1
假设我们div
不知道div
.
非常感谢任何帮助。
谢谢
解决方案
推荐阅读
- r - 使用逻辑表达式替换列表中的矩阵
- python - PyCharm 不输出所需的数据
- react-native - 反应导航 v5 在屏幕上获取 initialParams 道具
- java - Selenium TestNG 页面对象模型框架中的问题
- javascript - 如何将 256 位二进制字符串编码为 base64?
- django - Django - ApiDetailView
- javascript - 为多语言站点获取静态资产的服务人员
- javascript - 无法通过 Javascript 点击更改类名
- python - 坚持检查获胜条件并定义在python游戏中移动的函数
- fortran - 此代码是否演示了 GFortran 中的错误?