简体   繁体   English

如何在YUI事件处理程序中获取DOM节点?

[英]How do I get the DOM node within a YUI event handler?

Given a generic handler bound to a series of links with YUI, how do I find out which link triggered the event? 给定一个与YUI的一系列链接绑定的通用处理程序,我如何找出哪个链接触发了该事件?

YUI().use('node', function (Y) {
    var list = Y.one('#studentList'), links;
    links = list.all('a');
    links.on('click', function (e) {
        alert(this.get('id')); // this just shows a comma delimited list of all ids
    });
});

I suppose I could bind each link individually instead of using the "on" idiom on the links list, but it seems odd to me that YUI would not provide access to the DOM node. 我想我可以单独绑定每个链接,而不是使用链接列表上的“ on”惯用语,但是对我来说,YUI无法提供对DOM节点的访问似乎很奇怪。 Digging into the event object shows several private fields that look like the DOM node, but surely there must be a safe way of doing this. 深入研究事件对象将显示几个看起来像DOM节点的私有字段,但是肯定有一种安全的方法可以做到这一点。

e.currentTarget appears to be what you're looking for: e.currentTarget似乎是您要找的东西:

links.on('click', function (e) {
    alert(e.currentTarget.get('id'));
});

From NodeList's on : NodeList的on

By default, the this object will be the NodeList that the subscription came from, not the Node that received the event . 默认情况下, this对象将是订阅来源的NodeList ,而不是接收事件的Node Use e.currentTarget to refer to the Node . 使用e.currentTarget引用Node

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

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