获取 .delegate 的选择器
在此代码中,我需要获取 DOM 对象,即 .tabs
委托所连接的原始选择器。
$(".tabs").delegate("li:not(.selected) a", "click", function () {
// ^
// |
// +-----------i need to get this dom object
var selector = $(this).selector; // <---- returns an empty string ?
return false;
});
如何确定 .tabs
是什么并访问该对象?
In this code I need to get the DOM object that is .tabs
the original selector that the delegate is wired up against.
$(".tabs").delegate("li:not(.selected) a", "click", function () {
// ^
// |
// +-----------i need to get this dom object
var selector = $(this).selector; // <---- returns an empty string ?
return false;
});
How to determine what .tabs
is and access the object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在有人找到更聪明的方法之前,解决方法可能是将事件处理程序附加到所选元素本身:
jQuery 保证事件处理程序按照它们附加的顺序执行。
或者您根本不使用
delegate
并自行测试选择器(这可能是最好的解决方案):更新:
原始< code>Event 对象可通过
event.originalEvent
获得。所以你可以这样做:但是似乎
currentTarget
仅在 IE9 中受支持,并且在较低的 IE 版本中没有替代方案。所以跨浏览器兼容程度最高的解决方案仍然是上面的那些。
Until someone finds a smarter way, a workaround could be to attach an event handler to the selected elements themselves:
jQuery guarantees that event handlers are executed in the order they are attached.
Or you don't use
delegate
at all and make the selector test yourself (this is probably the best solution):Update:
The original
Event
object is available viaevent.originalEvent
. So you could do:But it seems
currentTarget
is only supported in IE9 and there is no alternative in lower IE versions.So the most cross-browser compatible solutions are still the ones above.
使用
.on()
方法,您可以通过将事件对象传递给函数并访问其delegateTarget
属性来访问委托目标。示例:
工作示例: http://jsfiddle.net/corydorning/ZzADh/
With the
.on()
method, you can access the delegated target by passing the event object to your function and accessing itsdelegateTarget
property.Example:
Working Example: http://jsfiddle.net/corydorning/ZzADh/