问题描述
我正在为jQuery编写一个插件,我希望这样做,以便用户可以以任何形式将数据传递给插件。 我解决了JSON或数组问题,但我无法确定数据是否是jQuery对象。
data = $('#list li');
console.debug( $.isPlainObject(data) ); // false
console.debug( $.isArray(data) ); // false
console.debug( data[0].tagName == "LI" ); // true, but see note below
最后一个方法返回true,但不保证用户正在为他们的数据使用LI标记,所以我想我需要这样的东西:
if ( $.isjQueryObject(data) ) { /* do something */ }
有谁知道更好的方法?
1楼
CMS
9
已采纳
2010-07-04 01:53:15
jQuery对象(或其别名$ )是一个普通的 ,所有jQuery对象都继承自jQuery.prototype对象(或其别名jQuery.fn )。
您可以使用运算符或方法检查其他对象的原型链中是否存在对象,例如:
function isjQueryObject(obj) {
return obj instanceof jQuery;
}
要么:
function isjQueryObject(obj) {
return jQuery.fn.isPrototypeOf(obj);
}
2楼
Colin O'Dell
1
2010-07-04 01:42:17
jQuery对象只是一个元素集合,存储为数组,附加了附加功能和内容。 所以基本上你可以像使用常规数组一样使用jQuery元素。
3楼
Ken Redler
1
2010-07-04 01:55:56
怎么样:
var isJq = data instanceof jQuery;