返回值:jQueryjQuery(expression, [context])

概述

这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组元素。

jQuery 的核心功能都是通过这个函数实现的。 jQuery中的一切都基于这个函数,或者说都是在以某种方式使用这个函数。这个函数最基本的用法就是向它传递一个表达式(通常由 CSS 选择器组成),然后根据这个表达式来查找所有匹配的元素。

默认情况下, 如果没有指定context参数,$()将在当前的 HTML document中查找 DOM 元素;如果指定了 context 参数,如一个 DOM 元素集或 jQuery 对象,那就会在这个 context 中查找。在jQuery 1.3.2以后,其返回的元素顺序等同于在context中出现的先后顺序。

参考文档中 选择器 部分获取更多用于 expression 参数的 CSS 语法的信息。

参数

expressionString

用来查找的字符串

context (可选)Element, jQuery

作为待查找的 DOM 元素集、文档或 jQuery 对象。

示例

描述:

找到所有 p 元素,并且这些元素都必须是 div 元素的子元素。

HTML 代码:
<p>one</p> <div><p>two</p></div> <p>three</p>
jQuery 代码:
$("div > p");
结果:
[ <p>two</p> ]

描述:

在文档的第一个表单中,查找所有的单选按钮(即: type 值为 radio 的 input 元素)。

jQuery 代码:
$("input:radio", document.forms[0]);

描述:

在一个由 AJAX 返回的 XML 文档中,查找所有的 div 元素。

jQuery 代码:
$("div", xml.responseXML);

目录 索引

输入要查找的关键字:

#id*.class:animated:button:checkbox:checked:contains:disabled:empty:enabled:eq:even:file:first-child:first:gt:has:header:hidden:hidden:image:input:last-child:last:lt:not:nth-child:odd:only-child:parent:password:radio:reset:selected:submit:text:visible[attrSel1][attrSel2][attrSelN][attribute!=value][attribute$=value][attribute*=value][attribute=value][attribute][attributeadd(expr, [context])addClass(class)addClass(function(index, class))after(content)after(function)ajaxComplete(callback)ajaxError(callback)ajaxSend(callback)ajaxStart(callback)ajaxStop(callback)ajaxSuccess(callback)ancestor descendantandSelf()animate(param,[dur],[e],[fn])animate(params, options)append(content)append(function(index, html))appendTo(content)attr(key, fn)attr(key, value)attr(name)attr(properties)before(content)before(function)bind(type, [data], fn)blur()blur(fn)change()change(fn)children([expr])clearQueue([queueName])click()click(fn)clone()clone(true)closest([expr])contents()contextcss(name)css(name, function(index, value))css(name, value)css(properties)data([name])data(name, value)data(obj)dblclick()dblclick(fn)delay(duration, [queueName])dequeue(name)detach([expr])die([type], [fn])each(callback)elementempty()end()eq(index)error()error(fn)fadeIn(speed, [callback])fadeOut(speed, [callback])fadeTo(speed, opacity, [fn])filter(expr)filter(fn)find(expr)first()focus()focus(fn)focusin( [fn] )focusout( [fn] )get()get(index)has(expr)hasClass(class)height()height(val)hide()hide(speed, [callback])hover(over, out)html()html(function(index, html))html(val)index(subject)innerHeight()innerWidth()insertAfter(content)insertBefore(content)is(expr)jQuery()jQuery(callback)jQuery(elements)jQuery(expr, [context])jQuery(html, [ownerDoc])jQuery(html, props)jQuery.ajax([options])jQuery.ajaxSetup([options])jQuery.boxModeljQuery.browser.versionjQuery.browserjQuery.contains(container, contained)jQuery.data([element], [key])jQuery.data(element, key, val)jQuery.each(object, [callback])jQuery.error(message)jQuery.extend([d],tgt,obj1,[objN])jQuery.extend(object)jQuery.fn.extend(object)jQuery.fx.offjQuery.get(url, [data], [fn], [type])jQuery.getJSON(url, [data], [fn])jQuery.getScript(url, [callback])jQuery.grep(array, fn, [invert])jQuery.inArray(value, array)jQuery.isArray(obj)jQuery.isEmptyObject(obj)jQuery.isFunction(obj)jQuery.isPlainObject(obj)jQuery.makeArray(obj)jQuery.map(array, callback)jQuery.merge(first, second)jQuery.noConflict()jQuery.noConflict(extreme)jQuery.noopjQuery.param(obj, [traditional])jQuery.parseJSON(json)jQuery.post(url, [data], [fn], [type])jQuery.proxy(function, scope)jQuery.supportjQuery.toArray()jQuery.trim(str)jQuery.unique(array)keydown()keydown(fn)keypress()keypress(fn)keyup()keyup(fn)last()lengthlive(type, [data], fn)load(url, [data], [callback])map(callback)mousedown(fn)mousemove(fn)mouseout(fn)mouseover(fn)mouseup(fn)next([expr])nextAll([expr])nextUntil([expr])not(expr)offset(coordinates)offsetParent()offsetone(type, [data], fn)outerHeight(options)outerWidth(options)parent > childparent([expr])parents([expr])parentsUntil([expr])position()prepend(content)prepend(function(index, html))prependTo(content)prev + nextprev ~ siblingsprev([expr])prevAll([expr])prevUntil([expr])queue(name ,callback)queue(name)queue(name, queue)ready(fn)remove([expr])removeAttr(name)removeClass(class | fn])removeClass(class)removeData(name)replaceAll(selector)replaceWith(content)resize(fn)scroll(fn)scrollLeft()scrollLeft(val)scrollTop()scrollTop(val)select()select(fn)selector1,selector2,selectorNselectorserialize()serializeArray()show()show(speed, [callback])siblings([expr])size()slice(start, [end])slideDown(speed, [callback])slideToggle(speed, [callback])slideUp(speed, [callback])stop([clearQueue], [gotoEnd])submit()submit(fn)text()text(function(index, text))text(val)toggle()toggle(fn, fn2, [fn3, fn4, ...])toggle(speed, [callback])toggle(switch)toggleClass(class)toggleClass(class, switch)toggleClass(function(index, class), [switch])trigger(type, [data])triggerHandler(type, [data])unbind([type], [data])unload(fn)unwrap()val()val(array)val(function(index, value))val(val)width()width(val)wrap(elem)wrap(fn)wrap(html)wrapAll(elem)wrapAll(html)wrapInner(elem)wrapInner(fn)wrapInner(html)
#id*.class:animated:button:checkbox:checked:contains:disabled:empty:enabled:eq:even:file:first-child:first:gt:has:header:hidden:hidden:image:input:last-child:last:lt:not:nth-child:odd:only-child:parent:password:radio:reset:selected:submit:text:visible[attrSel1][attrSel2][attrSelN][attribute!=value][attribute$=value][attribute*=value][attribute=value][attribute][attributeadd(expr, [context])addClass(class)addClass(function(index, class))after(content)after(function)ajaxComplete(callback)ajaxError(callback)ajaxSend(callback)ajaxStart(callback)ajaxStop(callback)ajaxSuccess(callback)ancestor descendantandSelf()animate(param,[dur],[e],[fn])animate(params, options)append(content)append(function(index, html))appendTo(content)attr(key, fn)attr(key, value)attr(name)attr(properties)before(content)before(function)bind(type, [data], fn)blur()blur(fn)change()change(fn)children([expr])clearQueue([queueName])click()click(fn)clone()clone(true)closest([expr])contents()contextcss(name)css(name, function(index, value))css(name, value)css(properties)data([name])data(name, value)data(obj)dblclick()dblclick(fn)delay(duration, [queueName])dequeue(name)detach([expr])die([type], [fn])each(callback)elementempty()end()eq(index)error()error(fn)fadeIn(speed, [callback])fadeOut(speed, [callback])fadeTo(speed, opacity, [fn])filter(expr)filter(fn)find(expr)first()focus()focus(fn)focusin( [fn] )focusout( [fn] )get()get(index)has(expr)hasClass(class)height()height(val)hide()hide(speed, [callback])hover(over, out)html()html(function(index, html))html(val)index(subject)innerHeight()innerWidth()insertAfter(content)insertBefore(content)is(expr)jQuery()jQuery(callback)jQuery(elements)jQuery(expr, [context])jQuery(html, [ownerDoc])jQuery(html, props)jQuery.ajax([options])jQuery.ajaxSetup([options])jQuery.boxModeljQuery.browser.versionjQuery.browserjQuery.contains(container, contained)jQuery.data([element], [key])jQuery.data(element, key, val)jQuery.each(object, [callback])jQuery.error(message)jQuery.extend([d],tgt,obj1,[objN])jQuery.extend(object)jQuery.fn.extend(object)jQuery.fx.offjQuery.get(url, [data], [fn], [type])jQuery.getJSON(url, [data], [fn])jQuery.getScript(url, [callback])jQuery.grep(array, fn, [invert])jQuery.inArray(value, array)jQuery.isArray(obj)jQuery.isEmptyObject(obj)jQuery.isFunction(obj)jQuery.isPlainObject(obj)jQuery.makeArray(obj)jQuery.map(array, callback)jQuery.merge(first, second)jQuery.noConflict()jQuery.noConflict(extreme)jQuery.noopjQuery.param(obj, [traditional])jQuery.parseJSON(json)jQuery.post(url, [data], [fn], [type])jQuery.proxy(function, scope)jQuery.supportjQuery.toArray()jQuery.trim(str)jQuery.unique(array)keydown()keydown(fn)keypress()keypress(fn)keyup()keyup(fn)last()lengthlive(type, [data], fn)load(url, [data], [callback])map(callback)mousedown(fn)mousemove(fn)mouseout(fn)mouseover(fn)mouseup(fn)next([expr])nextAll([expr])nextUntil([expr])not(expr)offset(coordinates)offsetParent()offsetone(type, [data], fn)outerHeight(options)outerWidth(options)parent > childparent([expr])parents([expr])parentsUntil([expr])position()prepend(content)prepend(function(index, html))prependTo(content)prev + nextprev ~ siblingsprev([expr])prevAll([expr])prevUntil([expr])queue(name ,callback)queue(name)queue(name, queue)ready(fn)remove([expr])removeAttr(name)removeClass(class | fn])removeClass(class)removeData(name)replaceAll(selector)replaceWith(content)resize(fn)scroll(fn)scrollLeft()scrollLeft(val)scrollTop()scrollTop(val)select()select(fn)selector1,selector2,selectorNselectorserialize()serializeArray()show()show(speed, [callback])siblings([expr])size()slice(start, [end])slideDown(speed, [callback])slideToggle(speed, [callback])slideUp(speed, [callback])stop([clearQueue], [gotoEnd])submit()submit(fn)text()text(function(index, text))text(val)toggle()toggle(fn, fn2, [fn3, fn4, ...])toggle(speed, [callback])toggle(switch)toggleClass(class)toggleClass(class, switch)toggleClass(function(index, class), [switch])trigger(type, [data])triggerHandler(type, [data])unbind([type], [data])unload(fn)unwrap()val()val(array)val(function(index, value))val(val)width()width(val)wrap(elem)wrap(fn)wrap(html)wrapAll(elem)wrapAll(html)wrapInner(elem)wrapInner(fn)wrapInner(html)