I want to know how to get selector from $(document) & $(window)?
el = $(document);
alert(el.selector); // return nothing, I want to output -> document
el = $(window);
alert(el.selector); // return nothing, I want to output -> window
Thank you very much!
I want to know how to get selector from $(document) & $(window)?
el = $(document);
alert(el.selector); // return nothing, I want to output -> document
el = $(window);
alert(el.selector); // return nothing, I want to output -> window
Thank you very much!
selector
property is for internal use only and should only be used for debugging, if that.
– Kevin B
Commented
Feb 15, 2013 at 18:33
alert(el.context);
? Selectors are always strings, and you're not passing any selectors to the jQuery function.
– jbabey
Commented
Feb 15, 2013 at 18:35
There is no selector, so there's nothing to get. You're passing a node.
A "selector" is a string of text that conforms to the Selectors API, which is the same API used by CSS. In JavaScript, the selectors are a subset of the API, or if using jQuery, there are proprietary extensions.
There's no selector value when you instantiate a jQuery object from a DOM element or something like window
.
If you just want to know if a jQuery object wraps document
or window
, do this:
if (theObject.length === 1 && theObject[0] === document) {
// it's $(document) ...
}
In fact you can also do this:
if (theObject.is(document)) {
or
if (theObject.is(window))
The .is()
function also works if you want to test for specific DOM elements too.
There is no selector. jQuery
is wrapping those DOM
element references in a jQuery
object.
For more information see jQuery source init