jQuery e.target 冒泡最佳实践?
在我的重型 ajax 代码中,我总是绑定“单击”body 标记和行为取决于 $(e.target)
&使用$.fn.hasClass()。但是,当我单击内部有 标记的锚点时,我的
$(e.target)
等于此节点,而不是我的父锚点想要它。
从现在开始,我已经使用了这个技巧 (var $t = $(e.target);
) :
/** bubbling **/
if($t.get(0).tagName !== "A" && $t.get(0).tagName !== "AREA") {
$t = $t.parent("a");
if(empty($t)) return true;
//else console.log("$t.bubble()", $t);
}
不知何故感觉不对......你有更好的实现吗?
$.fn.live()
没有解决我的问题,因为它仍然返回跨度作为目标。此外,我正在寻找速度(在基于原子的触摸设备上运行)和速度(在基于原子的触摸设备上运行)。 live 似乎慢得多(两次): http://jsperf.com/bind-vs-click /3
事实上,正如 @Guffa 指出的那样,使用 $.fn.live()
解决了跨度冒泡问题,因为我不需要 event.target
不再了。我想这里没有其他“正确”的答案(在容器上使用绑定)。
In my heavy-ajax code, i always bind "click" the body tag & act depending on $(e.target)
& using $.fn.hasClass()
. However when i click on an anchor that has a </span>
tag inside, my $(e.target)
equals this node, and not the parent anchor as i would like it to.
From now on, i have used this trick (var $t = $(e.target);
) :
/** bubbling **/
if($t.get(0).tagName !== "A" && $t.get(0).tagName !== "AREA") {
$t = $t.parent("a");
if(empty($t)) return true;
//else console.log("$t.bubble()", $t);
}
It feels wrong somehow... Do you have any better implementation ?
$.fn.live()
does not solve my issue as it still returns the span as the target. Moreover i'm looking for speed (running on atom-based touch devices) & live appeared to be way slower (twice) : http://jsperf.com/bind-vs-click/3
In fact, as @Guffa pointed, using $.fn.live()
solves the span bubbling issue as i don't need the event.target
anymore. I guess there is no other "right" answer here (using bind on a container).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为什么不使用为您完成此操作的
live
方法呢?live
方法绑定到 body 元素并根据选择器检查事件目标。jQuery:实时
Why not use the
live
method that does it for you?The
live
method binds to the body element and checks the event target against the selector.jQuery: live
closest
在这里很有用(尽管其他方法 -live< /code> 和
delegate
— 如下所述):从
$t
开始,向上遍历父元素链,直到找到与给定选择器匹配的元素。 — 在本例中为“a”。这是一个基本示例:
HTML:
JavaScript using jQuery:
Live copy
单击链接会在链接周围放置边框,无论您单击其中一个跨度还是
em
元素或任何位置。有点离题,但 jQuery 的最新版本通过
live
(它基本上执行您所做的操作,将事件挂接到主体上,然后查看它发生在哪个实际元素上,将选择器应用于元素匹配)和delegate
(与live
相同,但植根于特定元素而不是文档本身)。closest
is useful here (though other methods —live
anddelegate
— are described below):That starts with
$t
and works upward through the chain of parent elements until it finds one matching the given selector — "a" in this case.Here's a rudimentary example:
HTML:
JavaScript using jQuery:
Live copy
Clicking the link puts a border around the link, whether you click in one of the spans or the
em
element or wherever.Somewhat off-topic, but recent versions of jQuery handle this for you via
live
(which basically does what you do, hooks an event on the body and then looks to see what actual element it happened on, applying a selector to element matching) anddelegate
(same aslive
but rooted in a specific element instead of the document itself).我认为您这样做是因为 bind 不适用于 AJAX 调用后插入的内容。我建议您使用 live 附加事件并允许 jQuery 处理此事件。
I presume you're doing this because bind does not work on content inserted after an AJAX call. I would recommend you use live to attach your events and allow jQuery to handle this.