如何从 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
How to get javascript control from JQuery object?
提问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] == this
if this
is a DOM element. If it's something else, e.g. a selector, $(this)[0]
is the way to go.
$(this)[0] == this
ifthis
是一个 DOM 元素。如果它是其他东西,例如选择器,$(this)[0]
就是要走的路。
回答by ünver K?z?lay
(function (e) {
var a = false;
try {
$(this)[0];
a = true;
} catch (h) {}
alert(a.toString());
})(window);