简体   繁体   English

JavaScript单击事件处理

[英]JavaScript Click Event Handling

So I attach an event to the document to watch when a click event happens on a specific anchor with a class yui3-pjax , in some instances the anchor has a span child element. 因此,我将一个事件附加到文档,以观察在具有类yui3-pjax的特定锚点上发生单击事件yui3-pjax ,在某些情况下,锚点具有span子元素。 How is it I can watch for just the click event bubbles to just the anchor? 我怎么能只看到点击事件气泡到锚点? My current if-statement for watching the event is: 我目前关于观看活动的if语句是:

document.addEventListener('click', function(evt) {
  if(evt.target.classList.contains('yui3-pjax') || evt.target.parentNode.classList.contains('yui3-pjax')) {
    // do stuff
  }
});

HTML snippets HTML片段

<a class="page_current yui3-pjax" href="/forum/chatterbox/last-person-to-reply-wins/t.94028885_991/" title="page 67">
  67
</a>
<a class="yui3-pjax cta-button-sm gray-button postitem-link pull-right" href="/forum/chatterbox/last-person-to-reply-wins/t.94028885_991/">
  <span>67</span>
</a>

I'm not sure if there's a better way than watching both the anchor and the span element's parent (the same anchor) without binding it to the anchor itself, but the problem with that is some of the anchors are dynamically generated, and this clearly won't work since it'll reference the document. 我不确定是否有更好的方法,而不是观察锚点和span元素的父节点(相同的锚点)而不将其绑定到锚点本身,但问题是一些锚点是动态生成的, this很明显因为它会引用该文档,所以不起作用。 Any ideas? 有任何想法吗?

Edit: I think there's some confusion about what I'm asking. 编辑:我认为对于我的要求存在一些困惑。 Let me clarify, I only want to have to check the anchor if it has the yui3-pjax class. 让我澄清一点,我只想检查锚是否有yui3-pjax类。 I'm not sure how to handle the propagation/bubbling of the event from the child or descendant element of that anchor to achieve that. 我不确定如何处理来自子锚或该锚的后代元素的事件的传播/冒泡以实现该目的。

Hope I'm understanding this correctly. 希望我能正确理解这一点。 Instead of manually checking the element and the parent for a certain class, I would instead write a function that recurses up the DOM to find out whether the event target is contained within an element with the given class. 我不是手动检查某个类的元素和父元素,而是编写一个函数来重新计算DOM,以确定事件目标是否包含在具有给定类的元素中。 This way, you don't have to write code that relies on the structure of the HTML, but that rather dynamically determines whether or not your element lives inside an element with (eg) class 'yui3-pjax'. 这样,您不必编写依赖于HTML结构的代码,而是动态地确定您的元素是否位于具有(例如)类'yui3-pjax'的元素内。 Note I am not checking if the element with class 'yui3-pjax' is an anchor, but you can add that in if needed. 注意我没有检查具有类'yui3-pjax'的元素是否是锚点,但是如果需要,可以添加它。 Check out the fiddle http://jsfiddle.net/9L0jce6x/ . 查看小提琴http://jsfiddle.net/9L0jce6x/

document.addEventListener('click', function(evt) {
    if(elementOrParentsHaveClass(evt.target, 'yui3-pjax')){
        //do stuff
    }
});

//Recurse up the DOM until we find a parent with the given class, or return false if we don't
function elementOrParentsHaveClass(elem, className){
    if(elem && elem.classList.contains(className)){
        return true;  //yay!
    }else{
        //Recurse only if parent exists
        if(elem.parentNode){
            return elementOrParentsHaveClass(elem.parentNode, className);
        }
        else{
            //If the parent node does not exist, end recursion - we finished recursing up the DOM
            return false;
        }
    }
}

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

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