简体   繁体   English

查找最近的href属性的最佳方法?

[英]Best way to find nearest href attribute?

Why I don't think it's a duplicate. 为什么我不认为这是重复的。

Hmm, I think that's slightly different to what I'm asking. 嗯,我认为这与我要问的稍有不同。 I don't want to add a listener to all A tags, rather, substract an href attribute, if any, on a click event's target. 我不想将监听器添加到所有A标签,而是在点击事件的目标上减去href属性(如果有)。

I'm trying to "Ajaxify" my whole site, so that when an user clicks ANY link contained within the page, a script sends the "url" or HREF attribute (of the clicked element) to an Ajax function, which in return, renders the requested content (as indicated by the link clicked). 我正在尝试“ Ajaxify”我的整个网站,以便当用户单击页面中包含的任何链接时,脚本会将“ URL”或HREF属性(所单击元素的属性)发送给Ajax函数,该函数将作为回报,呈现请求的内容(如单击的链接所示)。

I need to find the HREF attribute of the clicked element, if the element is a link. 如果该元素是链接,则需要找到clicked元素的HREF属性。 The problem here , is that many elements can be contained within an A tag (because of the way I've structured them), and e.target.href doesn't necessarily always return an HREF attribute. 这里的问题是,A标记中可以包含许多元素(由于我的结构方式),而e.target.href不一定总是返回HREF属性。

Here is what I have so far: 这是我到目前为止的内容:

function ajaxifyLinks(e) {
    var target = e.target;
    e.preventDefault();
    while(!target.href) {
        target = target.parentNode;
    }
    if(target.href) {
        ajaxLoad(target.href);
    }
}
document.body.addEventListener('click', ajaxifyLinks);

And here are examples of different "clickable" links that I have: 以下是我拥有的不同“可点击”链接的示例:

<!-- Link -->
<a href="/cats">
   cats
</a>

<!-- Link -->
<a href="/hello">
   <span>
      <span> hi </span>
   </span>
</a>

<!-- Link -->
<a href="/bye">
   <span>
      bye
   </span>
</a>

As you can see, this is why e.target.href won't always return the HREF attribute, because you are actually clicking a "linked" span element, however, the browser does take you to the link. 如您所见,这就是为什么e.target.href并不总是返回HREF属性的原因,因为您实际上是在单击“链接”的span元素,但是,浏览器确实将您带到了该链接。 Why does this happen? 为什么会这样? And is there any way I can benefit from that behavior? 我有什么办法可以从这种行为中受益? (As in, extracting the location where the browser is taking you, even if you aren't clicking over an A tag). (例如,提取浏览器将带您到的位置,即使您没有单击A标签也是如此)。

I don't like my solution, because the while loop just keeps looking up the DOM tree, sometimes needlessly (when e.target isn't a link or contained by a link). 我不喜欢我的解决方案,因为while循环有时会一直在不必要的情况下查找DOM树(当e.target不是链接或包含在链接中时)。

Thanks. 谢谢。

If the element clicked does not have an href, it searches it's parents for an element that has an href. 如果单击的元素没有href,它将在其父级中搜索具有href的元素。 Vanilla JS solution. 香草JS解决方案。

 function findUpTag(el, attr) { while (el.parentNode) { el = el.parentNode; if (el[attr]) { return el; } } return null; } document.body.onclick = function (event) { event.preventDefault(); var href = event.target.href; if (!href) { var closest = findUpTag(event.target, 'href'); if (closest) { href = closest.href; } } document.getElementById('output').innerHTML = 'element clicked: ' + event.target.nodeName + '<br>closest href: ' + href; }; 
 a, output { display: block; } 
 <!-- Link --> <a href="/cats"> cats </a> <!-- Link --> <a href="/hello"> <span> <span> hi </span> </span> </a> <!-- Link --> <a href="/bye"> <span> bye </span> </a> <output id="output"></output> 

New answer: 新答案:

 <!-- Link --> <a href="/cats"> cats </a> <!-- Link --> <a href="/hello"> <span style="pointer-events: none;"> <span style="pointer-events: none;"> hi </span> </span> </a> <!-- Link --> <a href="/bye"> <span style="pointer-events: none;"> bye </span> </a> 

Use the .parent() , ref: http://api.jquery.com/parent/ 使用.parent() ,参考: http : .parent()

for example, you can do 例如,您可以

 var hrefElem = $(target).parent('*[href]'); var link = hrefElem.attr('href'); 

Just attach a click handler to each link on the page: 只需将点击处理程序附加到页面上的每个链接即可:

function ajaxify(e)
{
    e.preventDefault();
    ajaxLoad(this.href);
}

[].forEach.call(document.getElementsByTagName('a'), function(el) {
    el.addEventListener('click', ajaxify.bind(el));
});

Have a look at What is event bubbling and capturing? 看看什么是事件冒泡和捕获? to understand how events propagate through the DOM. 了解事件如何通过DOM传播。

The technique of capturing anchor links and loading them via Ajax is referred to as Hijax . 捕获锚链接并通过Ajax加载锚链接的技术称为Hijax Because you're replacing content on the page, you can't simply set up a single event to handle all links, and you probably don't want to keep re-binding every page load, so the approach you are using is good, and is known as event delegation . 因为您要替换页面上的内容,所以不能简单地设置一个事件来处理所有链接,并且您可能不想保持每次页面加载时的重新绑定,所以您使用的方法很好,被称为事件委托

The last article describes exactly your problem (right at the very end), and also describes a solution similar to yours, which is really the best way to do it in this scenario. 上一篇文章恰好描述了您的问题(恰好在最后),还描述了与您的问题类似的解决方案,这实际上是解决此问题的最佳方法。 However, you could look at using a microlibrary to simplify some of the event delegation; 但是,您可以考虑使用微库来简化某些事件委托。 one example is gator.js . 一个例子是gator.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM