例如,我需要检查当前上下文是否是jQuery堆栈。你会怎么做?忽略方法检查。这是个丑陋的解决方案。:)
例如:
if(it_is_jquery_stack_context) {
// do some stuff with jQuery support
}
else {
// do some stuff without it
}更新
例如:
if(typeof this.get=='function' && this.length && this.get(0)===dom_ element) {
// do some stuff
}最后一个问题是:
如何检查this.get(0)是否是dom元素的实例?
发布于 2011-11-15 16:52:13
实际上,这里只是猜测您的问题,但是如果您想检查this是否引用了jQuery对象,您可以这样做:
if( "jquery" in this ) { //lowercase intended
// do some stuff with jQuery support
}
else {
// do some stuff without it
}注意,jQuery对象指的是jQuery对象,而不是jQuery命名空间。正如在"jquery" in jQuery中一样,"jquery" in jQuery("div")是假的,但"jquery" in jQuery("div")是真的。
最后一个问题是:
如何检查this.get(0)是否是dom元素的实例?
例如:
this[0] && this[0].nodeType && this[0].nodeType === 1但是这是无用的,因为jQuery对象中并不总是有dom节点。
发布于 2011-11-15 16:52:24
您可以这样做,以检查this,看看它是否是jQuery对象:
if (this.jquery)
{
//this is a jquery object so you can act accordingly
}
else
{
//this is not a jQuery object, you can do $(this) to make it into one if it's a DOM object
}发布于 2011-11-15 16:47:21
您可以执行以下任一操作:
if (jQuery) {
// jQuery is loaded
} else {
// jQuery is not loaded
}或
if (typeof jQuery == 'undefined') {
// jQuery is not loaded
} else {
// jQuery is loaded
}https://stackoverflow.com/questions/8139906
复制相似问题