直到有人发现了一个更聪明的办法,解决方法可能是一个事件处理程序附加到所选元素本身:
$('.tabs').click(function(event) {
event.root = this; // or whatever name suits you best
});
$(".tabs").delegate("li:not(.selected) a", "click", function (event) {
// event.root contains the DOM element
});
jQuery的保证事件处理程序的顺序执行它们所连接。
,或者您没有在所有使用delegate
,并选择测试自己(这可能是最好的解决办法):
$('.tabs').click(function(event) {
if($(event.target).is("li:not(.selected) a")) {
// this refers to the .tab DOM element
// event.target refers to the originally clicked element
}
});
更新:
原Event
对象可通过event.originalEvent
获取。所以,你可以这样做:
$(".tabs").delegate("li:not(.selected) a", "click", function (event) {
var dom = event.originalEvent.currentTarget;
});
但似乎currentTarget
仅在IE9支持,并有较低的IE版本别无选择。
因此,大多数跨浏览器兼容的解决方案仍然是上面的那些。
为什么需要选择? – BoltClock 2011-02-10 10:30:00
选择器或甚至任何。只要我能以'匿名'的方式访问.tabs,并且可能比li-a的父级遍历更好。页面上有不同的选项卡,需要处理有点不同。 – 2011-02-10 10:34:02