获得原始目标的最佳方式 [英] Best way to get the Original Target

查看:160
本文介绍了获得原始目标的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery(或者一般的浏览器JavaScript)中获取事件的原始目标是什么样的jQuery和/或最佳实践方法。

What's a jQuery like and/or best practices way of getting the original target of an event in jQuery (or in browser javascript in general).

我一直在使用这样的东西

I've been using something like this

$('body').bind('click', function(e){
        //depending on the browser, either srcElement or 
        //originalTarget will be populated with the first
        //element that intercepted the click before it bubbled up
        var originalElement = e.srcElement;
        if(!originalElement){originalElement=e.originalTarget;}                         
});

哪些工作,但我不满意两行功能嗅探。有没有更好的方法?

which works, but I'm not pleased with the two line feature sniffing. Is there a better way?

推荐答案

你可以用 var originalElement = e。 srcElement || e.originalTarget; 但它不是很漂亮的JQuery像; - )

You can do it in one line with var originalElement = e.srcElement || e.originalTarget; but it ain't pretty JQuery-like ;-)

这篇关于获得原始目标的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆