如何从 JQuery 对象获取 javascript 控件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5388306/
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-25 17:01:53  来源:igfitidea点击:

How to get javascript control from JQuery object?

javascriptjquery

提问by Homam

I'm a beginner in JQuery,

我是 JQuery 的初学者,

How can I get the control as a javascript object from a JQuery object

如何从 JQuery 对象中将控件作为 javascript 对象获取

var _object = $(this). ??

回答by Z. Zlatev

Most common var _object = $(this)[0];

最常见的 var _object = $(this)[0];

If you have more than 1 elements matched: $(this)[0], $(this)[1], $(this)[2]and so on.

如果有匹配的超过1个元素:$(this)[0]$(this)[1]$(this)[2]等。

$(this).get()is also possible. It's only advantage over the array model is that it allows selection of the kind $(this).get(-1)where it gets you the last matched object

$(this).get()也是可以的。与数组模型相比,它的唯一优势在于它允许选择$(this).get(-1)可以为您提供最后匹配对象的类型

回答by Richard Dalton

var _object = $(this)[0];

I think this is right, can't check though because I'm on my phone.

我认为这是对的,但无法检查,因为我在使用手机。

回答by ThiefMaster

In your case, simply use this.

在您的情况下,只需使用this.

$(this)[0] == thisif thisis a DOM element. If it's something else, e.g. a selector, $(this)[0]is the way to go.

$(this)[0] == thisifthis是一个 DOM 元素。如果它是其他东西,例如选择器,$(this)[0]就是要走的路。

回答by ünver K?z?lay

(function (e) {
    var a = false;
    try {
        $(this)[0];
        a = true;
    } catch (h) {}
    alert(a.toString());
})(window);