admin管理员组文章数量:1022719
I'm writing a plugin for jQuery and I want to make it so the user can pass data to the plugin in any form. I have the JSON or array problem worked out, but I'm having trouble trying to determine if the data is a jQuery object.
data = $('#list li');
console.debug( $.isPlainObject(data) ); // false
console.debug( $.isArray(data) ); // false
console.debug( data[0].tagName == "LI" ); // true, but see note below
The last method returns true, but there is no guarantee that the user is using an LI
tag for their data, so I think I need something like this:
if ( $.isjQueryObject(data) ) { /* do something */ }
Does anyone know a better method?
I'm writing a plugin for jQuery and I want to make it so the user can pass data to the plugin in any form. I have the JSON or array problem worked out, but I'm having trouble trying to determine if the data is a jQuery object.
data = $('#list li');
console.debug( $.isPlainObject(data) ); // false
console.debug( $.isArray(data) ); // false
console.debug( data[0].tagName == "LI" ); // true, but see note below
The last method returns true, but there is no guarantee that the user is using an LI
tag for their data, so I think I need something like this:
if ( $.isjQueryObject(data) ) { /* do something */ }
Does anyone know a better method?
Share Improve this question edited Jul 4, 2010 at 2:29 Christian C. Salvadó 829k185 gold badges928 silver badges845 bronze badges asked Jul 4, 2010 at 1:39 MottieMottie 86.5k30 gold badges130 silver badges248 bronze badges 1- Apparently, there is a Ben Alman plugin for that - benalman./projects/jquery-misc-plugins/#isjquery – Mottie Commented Oct 9, 2010 at 16:25
3 Answers
Reset to default 9The jQuery
object (or its alias $
) is a plain constructor function, all jQuery objects inherit from the jQuery.prototype
object (or its alias jQuery.fn
).
You can check if an object exists in the prototype chain of other object, by using either the instanceof
operator or the isPrototypeOf
method, for example:
function isjQueryObject(obj) {
return obj instanceof jQuery;
}
Or:
function isjQueryObject(obj) {
return jQuery.fn.isPrototypeOf(obj);
}
The jQuery object is simply a collection of elements, stored as an array, with additional functions and stuff attached. So essentially you could use the jQuery elements just like you would a regular array.
How about:
var isJq = data instanceof jQuery;
I'm writing a plugin for jQuery and I want to make it so the user can pass data to the plugin in any form. I have the JSON or array problem worked out, but I'm having trouble trying to determine if the data is a jQuery object.
data = $('#list li');
console.debug( $.isPlainObject(data) ); // false
console.debug( $.isArray(data) ); // false
console.debug( data[0].tagName == "LI" ); // true, but see note below
The last method returns true, but there is no guarantee that the user is using an LI
tag for their data, so I think I need something like this:
if ( $.isjQueryObject(data) ) { /* do something */ }
Does anyone know a better method?
I'm writing a plugin for jQuery and I want to make it so the user can pass data to the plugin in any form. I have the JSON or array problem worked out, but I'm having trouble trying to determine if the data is a jQuery object.
data = $('#list li');
console.debug( $.isPlainObject(data) ); // false
console.debug( $.isArray(data) ); // false
console.debug( data[0].tagName == "LI" ); // true, but see note below
The last method returns true, but there is no guarantee that the user is using an LI
tag for their data, so I think I need something like this:
if ( $.isjQueryObject(data) ) { /* do something */ }
Does anyone know a better method?
Share Improve this question edited Jul 4, 2010 at 2:29 Christian C. Salvadó 829k185 gold badges928 silver badges845 bronze badges asked Jul 4, 2010 at 1:39 MottieMottie 86.5k30 gold badges130 silver badges248 bronze badges 1- Apparently, there is a Ben Alman plugin for that - benalman./projects/jquery-misc-plugins/#isjquery – Mottie Commented Oct 9, 2010 at 16:25
3 Answers
Reset to default 9The jQuery
object (or its alias $
) is a plain constructor function, all jQuery objects inherit from the jQuery.prototype
object (or its alias jQuery.fn
).
You can check if an object exists in the prototype chain of other object, by using either the instanceof
operator or the isPrototypeOf
method, for example:
function isjQueryObject(obj) {
return obj instanceof jQuery;
}
Or:
function isjQueryObject(obj) {
return jQuery.fn.isPrototypeOf(obj);
}
The jQuery object is simply a collection of elements, stored as an array, with additional functions and stuff attached. So essentially you could use the jQuery elements just like you would a regular array.
How about:
var isJq = data instanceof jQuery;
本文标签: javascriptDetecting jQuery ObjectStack Overflow
版权声明:本文标题:javascript - Detecting jQuery Object - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745576055a2157022.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论