javascript 如何在 Jquery 中的 event.currentTarget 中选择/查找元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13378051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 18:36:07  来源:igfitidea点击:

How to select/find an element inside an event.currentTarget in Jquery?

javascriptjqueryjavascript-eventsbinding

提问by frequent

I'm using this:

我正在使用这个:

$('.sizeChart').on('vclick', '.entry .ui-btn', function(e){

        console.log( e )
        console.log( e.currentTarget )
        console.log( $( e.currentTarget )
        console.log( $( e.currentTarget ).find('input.qtyInput') )

    var qty = $( e.currentTarget ).find('input.qtyInput');
    // do something

 });

Which works, but $( e.currentTarget ).find(...)seems awkward to me.

哪个有效,但$( e.currentTarget ).find(...)对我来说似乎很尴尬。

I can't bind directly to the inputbecause this binding will go dead on iOS3+4 after a couple of clicks. Binding to the closest ui-btnworks throughout.

我无法直接绑定到 ,input因为单击几次后,此绑定将在 iOS3+4 上失效。ui-btn始终绑定到最接近的作品。

Question:
Is there a better/easier/faster way to do the binding than what I'm using ?

问题:
是否有比我使用的更好/更容易/更快的绑定方式?

回答by Felix Kling

You can just use thisinstead of e.currentTarget:

您可以使用this代替e.currentTarget

$(this).find(...);

Proof that event.currentTargetand thisare the same.

证明event.currentTargetthis是一样的。

Also the documentationsays:

另外,文件说:

This property will typically be equal to the thisof the function.

此属性通常等于this函数的 。



That's about it. It is pretty common to pass a DOM element directly to jQuery and use DOM traversal methods on it.

就是这样。将 DOM 元素直接传递给 jQuery 并在其上使用 DOM 遍历方法是很常见的。