单击时如何确定一系列元素中元素的 DOM 位置

发布于 2024-12-06 14:41:26 字数 1038 浏览 0 评论 0原文

在这个场景中,我有一系列列表项,每个列表项都有一个相应的内容区域。当点击列表项时,对应的内容区域被操纵(即,如果点击第一列表项,则将操纵第一内容部分)。

<ul>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
</ul>

<div>
    <section>Content section</section>
    <section>Content section</section>
    <section>Content section</section>
</div>

我的老派做法是给每个列表项和部分一个 id,例如“li1”、“li2”等和“section1”、“section2”等。然后我将解析 id 中的整数被单击的元素并操作相应的部分。

有没有一种方法可以确定这一点而不需要额外的 id 属性?例如,如果我单击第三个列表项并知道这是第三个,我可以使用 document.querySelector('div:nth-child(3)') 来操作第三个内容部分。我的问题是如何知道它是开始单击的系列中的第三个元素。

我首先想到的解决方案是这样的:

var target = e.target;
var parent = e.target.parentNode;

for (var i in parent.childNodes) {
    if (parent.childNodes[i].nodeType == 1 && parent.childNodes[i] == target) {
        // found it... i+1
    }
}

与仅使用 ID 相比,这似乎是一个相当昂贵的操作,特别是如果有更多的列表项和内容部分。我希望有一些节点属性可以为我提供我尚未找到的正确 DOM 位置。

欢迎现代纯浏览器解决方案。

In this scenario, I have a series of list items each of which has a corresponding content area. When clicking a list item, a corresponding content area is manipulated (i.e., if the first list item is clicked, then the first content section would be manipulated).

<ul>
    <li>List item</li>
    <li>List item</li>
    <li>List item</li>
</ul>

<div>
    <section>Content section</section>
    <section>Content section</section>
    <section>Content section</section>
</div>

My old-school way of doing this was giving each list item and section an id, such as "li1", "li2", etc. and "section1", "section2", etc. I would then parse the integer off the id of the element that was clicked and manipulate the corresponding section.

Is there a way to determine this without needing extra id attributes? E.g., if I click the 3rd list item and know that is the 3rd, I can use document.querySelector('div:nth-child(3)') to manipulate the third content section. My question is how to know it was the 3rd element in a series that was clicked to begin with.

My first-thought solution was something like this:

var target = e.target;
var parent = e.target.parentNode;

for (var i in parent.childNodes) {
    if (parent.childNodes[i].nodeType == 1 && parent.childNodes[i] == target) {
        // found it... i+1
    }
}

This seems like a rather expensive operation compared to just using IDs, especially if there were many more list items and content sections. I'm hoping there is some node attribute that will give me the correct DOM position that I haven't yet found.

Modern browser-only solutions welcomed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

穿越时光隧道 2024-12-13 14:41:26

所以我不知道你在这里做什么,但必须有一种更加面向数据的方法。
就像 li 和该部分都引用相同的产品或人员或其他内容一样,因此您可以通过该引用找到它。

否则你可以使用 previousElementSibling 方法来计算你的位置,就像这样

var position = function(el) {
  var count = 1;
  for(var cur = el.previousElementSibling; 
          cur !== null; cur = cur.previousElementSibling) {
    count++;
  }
  return count;
};

gl

So i have no ide what you are doing here but there must be a more data oriented approach to this.
Like both the li and the section is referring to the same Product or Person or something so you can find it by that reference.

otherwise you can use the previousElementSibling method to count your location like this

var position = function(el) {
  var count = 1;
  for(var cur = el.previousElementSibling; 
          cur !== null; cur = cur.previousElementSibling) {
    count++;
  }
  return count;
};

gl

网白 2024-12-13 14:41:26

我会使用类似的东西:

var target = e.target,
    i = 0;
while(target = target.previousSibling) {
    i += target.nodeType == 1;
}
alert('you clicked on '+i);

Fiddle: http://jsfiddle.net/K5Qg9/1/

你还可以尝试使用数据库或将内容分配给元素 expano onload:

var elems = document.getElementsByTagName('li'),
    i=0;
for(; elems[i]; i++) {
    elems[i].rel = i;
}

然后只需获取 e.target.rel onclick。小提琴:http://jsfiddle.net/UnrCt/

I’d use something like:

var target = e.target,
    i = 0;
while(target = target.previousSibling) {
    i += target.nodeType == 1;
}
alert('you clicked on '+i);

Fiddle: http://jsfiddle.net/K5Qg9/1/

You can also try using a data lib or assign stuff to the element expano onload:

var elems = document.getElementsByTagName('li'),
    i=0;
for(; elems[i]; i++) {
    elems[i].rel = i;
}

Then just fetch e.target.rel onclick. Fiddle: http://jsfiddle.net/UnrCt/

疏忽 2024-12-13 14:41:26

如果您可以使用 jQuery:$(elem).index()

If you can use jQuery: $(elem).index()

原来是傀儡 2024-12-13 14:41:26

更新 2016

好吧,近 5 年后我再次遇到了这个问题,只是这次我使用了一个更简单的解决方案,使用内置的数组方法:

var index = Array.prototype.indexOf.call(event.target.parent.children, event.target);

Update 2016

Well I've run into this issue again nearly 5 years later only this time I used a much simpler solution using a built-in Array method:

var index = Array.prototype.indexOf.call(event.target.parent.children, event.target);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文