我怎么知道,窗口是否在jQuery集中? [英] How can I tell, if window is in a jQuery set?

查看:51
本文介绍了我怎么知道,窗口是否在jQuery集中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的jQuery插件在 $(窗口)选择中调用时表现不同。如果窗口在集合中,我该如何检查?到目前为止我的尝试:

I want my jQuery plugin to behave differently, when called on a $(window) selection. How can I check, if window is in the set? My tries so far:

>>> $(window) == $(window)
false
>>> $(window).is(window)
false
>>> $(window).filter(window).length
0

(这些都可以正常工作对于文件,顺便说一下。)当然我总是可以检查这[0] ,但是有边缘情况喜欢 $('a')。添加(窗口)这种方法无法满足。

(Those all work fine for document, by the way.) Of course I can always inspect this[0], but there are edge cases like $('a').add(window) that are not catered by this approach.

我可以也可以通过功能过滤:

I can also filter via function:

>>> $(window).filter(function() { return this === window; }).length
1

但我希望更多'jQuery-ish'或通用方法。

but I was hoping for a bit more 'jQuery-ish' or generic approach.

推荐答案

你可以使用 index()并将其传递给窗口宾语。如果窗口对象不是该集合的一部分,或者其内部的从零开始的索引,它将返回 -1 另外设置:

You can use index() and pass it the window object. It will return -1 if the window object is not part of the set, or its zero-based index within the set otherwise:

>>> $(window).index(window) >= 0
true

>>> $("a").add(window).index(window) >= 0
true

>>> $("a").index(window) >= 0
false

这篇关于我怎么知道,窗口是否在jQuery集中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆