返回值:jQueryjQuery(selector, [context])
Accepts a string containing a CSS selector which is then used to match a set of elements.
-
1.0 新增jQuery(selector, [context])
selector (selector) A string containing a selector expressioncontext (Element, jQuery) 可选参数,A DOM Element, Document, or jQuery to use as context -
1.0 新增jQuery(element)
element (Element) A DOM element to wrap in a jQuery object. -
1.0 新增jQuery(elementArray)
elementArray (Array) An array containing a set of DOM elements to wrap in a jQuery object. -
1.0 新增jQuery(jQuery object)
jQuery object (Object) An existing jQuery object to clone. -
1.4 新增jQuery()
In the first formulation listed above, jQuery()
— which can also be written as $()
— searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references
these elements:
$('div.foo');
Selector Context
By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can
be given for the search by using the optional second parameter to the $()
function. For example, if within a callback function we wish to do a search for an element, we can restrict that search:
$('div.foo').click(function() { $('span', this).addClass('bar'); });
Since we've restricted the span selector to the context of this
, only spans within the clicked element will get the additional class.
Internally, selector context is implemented with the .find()
method, so $('span', this)
is equivalent to $(this).find('span')
.
Using DOM elements
The second and third formulations of this function allow us to create a jQuery object using a DOM element or elements that
we have already found in some other way. A common use of this facility is to call jQuery methods on an element that has been
passed to a callback function through the keyword this
:
$('div.foo').click(function() { $(this).slideUp(); });
This example causes elements to be hidden with a sliding animation when clicked. Because the handler receives the clicked
item in the this
keyword as a bare DOM element, the element must be wrapped in a jQuery object before we can call jQuery methods on it.
When XML data is returned from an Ajax call, we can use the $()
function to wrap it in a jQuery object that we can easily work with. Once this is done, we can retrieve individual elements
of the XML structure using .find()
and other DOM traversal methods.
Cloning jQuery Objects
When a jQuery object is passed as a parameter to the $()
function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.
Returning an Empty Set
As of jQuery 1.4, calling the jQuery()
method with no arguments returns an empty jQuery set. In previous versions of jQuery, this would return a set containing the document node.
示例:
Finds all p elements that are children of a div element.
<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<p>one</p> <div><p>two</p></div> <p>three</p>
<script>
$("div > p").css("border", "1px solid gray");
</script>
</body>
</html>
演示:
结果:
[ <p>two</p> ]
示例:
Finds all inputs of type radio within the first form in the document.
jQuery 代码:
$("input:radio", document.forms[0]);
示例:
Finds all div elements within an XML document from an Ajax response.
jQuery 代码:
$("div", xml.responseXML);
示例:
Sets the background color of the page to black.
jQuery 代码:
$(document.body).css( "background", "black" );
示例:
Hides all the input elements within a form.
jQuery 代码:
$(myForm.elements).hide()