What does the jQuery() function do?
In continuation to the question https://stackoverflow.com/questions/1452380/good-jquery-interview-questions-closed
I have a follow up question. Wh开发者_如何学运维at does the jQuery() function do? (there are four answer to this; subject should at least describe the selector-and-element-set mode)
I tried with googling but could not get a clear answer.
Thanks for any help in advance.
The jQuery
function can handle several types of input:
$("")
,$(null)
, or$(undefined)
$(DOMElement)
$(html)
->$(array)
$("#id")
$("TAG")
$(expr, $(...))
$(expr, context)
$(function)
Take a look at the source code of jQuery
and jQuery.fn.init
respectively to see how these are handled.
It does different things depending on what you pass to it:
jQuery(String query [, DOMElement context])
jQuery(String query [, jQueryResultSet context])
This will read the string as a query (eg: #foo > .bar a
). It will be run in the context of context
if it is specified, otherwise it is taken from document
.
jQuery(DOMElement node)
This converts the node into a jQuery result set containing that node. This is used mostly when you have a reference to an element (eg: in an event handler) and you wish to perform jQuery functions upon it.
jQuery(Function readyHandler)
This is a shortcut form of this:
jQuery(document).ready(Function readyHandler)
Running all your jQuery functions once the document has been loaded is so common, this shortcut was added.
jQuery('')
jQuery(null)
This selects the document.
Have a look at this article, it might help you: http://simonwillison.net/2007/Aug/15/jquery/ Particularly, look at the "Doing stuff with them" paragraph.
All in this document : http://docs.jquery.com/Core
精彩评论