javascript - js how to get selector from $(document) & $(window)? - Stack Overflow

admin2025-04-20  0

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!

Share Improve this question asked Feb 15, 2013 at 18:32 MicahMicah 4,5609 gold badges32 silver badges40 bronze badges 3
  • the 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
  • do you mean 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
  • If you could elaborate a little on why you need to do this, you may get some helpful advice. – Pointy Commented Feb 15, 2013 at 19:22
Add a ment  | 

3 Answers 3

Reset to default 3

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745081817a283904.html

最新回复(0)