获取点击的<a>的DOM路径 [英] Get the DOM path of the clicked <a>

查看:17
本文介绍了获取点击的<a>的DOM路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HTML

<body>
<div class="lol">
<a class="rightArrow" href="javascriptVoid:(0);" title"Next image">
</div>
</body>

伪代码

$(".rightArrow").click(function() {
rightArrowParents = this.dom(); //.dom(); is the pseudo function ... it should show the whole
alert(rightArrowParents);
});

警报消息是:

body div.lol a.rightArrow

我怎样才能用 javascript/jquery 得到这个?

How can I get this with javascript/jquery?

推荐答案

使用 jQuery,像这样(后面是一个除了事件不使用 jQuery 的解决方案;如果这很重要,函数调用会少很多):

Using jQuery, like this (followed by a solution that doesn't use jQuery except for the event; lots fewer function calls, if that's important):

$(".rightArrow").click(function() {
  var rightArrowParents = [];
  $(this).parents().addBack().not('html').each(function() {
    var entry = this.tagName.toLowerCase();
    if (this.className) {
      entry += "." + this.className.replace(/ /g, '.');
    }
    rightArrowParents.push(entry);
  });
  alert(rightArrowParents.join(" "));
  return false;
});

实例:

$(".rightArrow").click(function() {
  var rightArrowParents = [];
  $(this).parents().addBack().not('html').each(function() {
    var entry = this.tagName.toLowerCase();
    if (this.className) {
      entry += "." + this.className.replace(/ /g, '.');
    }
    rightArrowParents.push(entry);
  });
  alert(rightArrowParents.join(" "));
  return false;
});

<div class="lol multi">
  <a href="#" class="rightArrow" title="Next image">Click here</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

(在实际示例中,我已将 div 上的 class 属性更新为 lol multi 以演示处理多个类.)

(In the live examples, I've updated the class attribute on the div to be lol multi to demonstrate handling multiple classes.)

使用 parents 来获取元素的祖先点击后,通过 not(因为您从 body 开始),然后循环为每个父项创建条目并将它们推送到数组中.然后我们使用 addBacka 添加回来进入集合,这也会将集合的顺序更改为您想要的(parents 是特殊的,它以与您想要的顺序相反的顺序为您提供父母,但随后 addBAck 将其放回 DOM 顺序).然后它使用 Array#join 创建以空格分隔的字符串.

That uses parents to get the ancestors of the element that was clicked, removes the html element from that via not (since you started at body), then loops through creating entries for each parent and pushing them on an array. Then we use addBack to add the a back into the set, which also changes the order of the set to what you wanted (parents is special, it gives you the parents in the reverse of the order you wanted, but then addBAck puts it back in DOM order). Then it uses Array#join to create the space-delimited string.

创建条目时,如果 className 上有任何内容,我们会用 . 替换空格以支持具有多个类的元素(<p class='foo bar'> 具有 className = "foo bar",因此该条目最终为 p.foo.bar).

When creating the entry, if there's anything on className we replace spaces with . to support elements that have more than one class (<p class='foo bar'> has className = "foo bar", so that entry ends up being p.foo.bar).

为了完整起见,这是 jQuery 可能会过度使用的地方之一,您可以通过走上 DOM 轻松做到这一点:

Just for completeness, this is one of those places where jQuery may be overkill, you can readily do this just by walking up the DOM:

$(".rightArrow").click(function() {
  var rightArrowParents = [],
    elm,
    entry;

  for (elm = this; elm; elm = elm.parentNode) {
    entry = elm.tagName.toLowerCase();
    if (entry === "html") {
      break;
    }
    if (elm.className) {
      entry += "." + elm.className.replace(/ /g, '.');
    }
    rightArrowParents.push(entry);
  }
  rightArrowParents.reverse();
  alert(rightArrowParents.join(" "));
  return false;
});

实例:

$(".rightArrow").click(function() {
  var rightArrowParents = [],
    elm,
    entry;

  for (elm = this; elm; elm = elm.parentNode) {
    entry = elm.tagName.toLowerCase();
    if (entry === "html") {
      break;
    }
    if (elm.className) {
      entry += "." + elm.className.replace(/ /g, '.');
    }
    rightArrowParents.push(entry);
  }
  rightArrowParents.reverse();
  alert(rightArrowParents.join(" "));
  return false;
});

<div class="lol multi">
  <a href="#" class="rightArrow" title="Next image">Click here</a>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

我们只使用标准 parentNode 属性 重复向上遍历树,直到我们用完父元素或看到 html 元素.然后我们反转我们的数组(因为它返回到您想要的输出),并加入它,我们就可以开始了.

There we just use the standard parentNode property of the element repeatedly to walk up the tree until either we run out of parents or we see the html element. Then we reverse our array (since it's backward to the output you wanted), and join it, and we're good to go.

这篇关于获取点击的&lt;a&gt;的DOM路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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