js如何从$(文档)&获取选择器$(窗口)?

时间:2013-02-15 18:32:07

标签: javascript jquery window selector document

我想知道如何从$(document)& $(窗口)?

el = $(document);

alert(el.selector); // return nothing, I want to output -> document

el = $(window);

alert(el.selector); // return nothing, I want to output -> window

非常感谢!

3 个答案:

答案 0 :(得分:3)

没有选择器,所以没有什么可以得到的。你正在传递一个节点。

“selector”是一个符合Selectors API的文本字符串,它与CSS使用的API相同。在JavaScript中,选择器是API的子集,或者如果使用jQuery,则有专有扩展。

答案 1 :(得分:2)

从DOM元素或类似window之类的实例化jQuery对象时,没有选择器值。

如果您只是想知道jQuery对象是否包装documentwindow,请执行以下操作:

if (theObject.length === 1 && theObject[0] === document) {
  // it's $(document) ...
}

事实上你也可以这样做:

if (theObject.is(document)) {

if (theObject.is(window))

如果您也想测试特定的DOM元素,.is()函数也可以使用。

答案 2 :(得分:1)

没有选择器。 jQuery将这些DOM元素引用包装在jQuery对象中。

有关详细信息,请参阅jQuery source init

相关问题