gpt4 book ai didi

javascript - 检查当前上下文是否是 jQuery 堆栈

转载 作者:行者123 更新时间:2023-12-02 19:59:55 25 4
gpt4 key购买 nike

例如,我需要检查当前上下文是否是 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 元素的实例?

最佳答案

实际上只是猜测你的问题,但如果你想检查 this 是否引用了一个 jQuery 对象,你可以这样做:

if( "jquery" in this ) { //lowercase intended
// do some stuff with jQuery support
}
else {
// do some stuff without it
}

请注意,我所说的 jQuery 对象是指 jQuery 对象,而不是 jQuery 命名空间。如 jQuery 中的 "jquery" 为 false,但 jQuery("div") 中的 "jquery" 是真的。

The last question is:

How to check if this.get(0) is instance of dom element?

例如:

this[0] && this[0].nodeType && this[0].nodeType === 1

但这没有用,因为 jQuery 对象中并不总是有 dom 节点。

关于javascript - 检查当前上下文是否是 jQuery 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8139906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com