jQuery API 返回首页目录 | jQuery API 中英文对照版
$(expr,context)
$(expr, context)

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

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

在默认情况下,$()查询的是当前HTML文档中的DOM元素。

返回值:jQuery

参数:

  • expr (String): 用来查询用的字符串
  • context (Element|jQuery): (可选)作为上下文的DOM元素、文档或jQuery对象。
 
示例:

找到所有是div元素子元素的p元素。

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])

示列:

查询指定XML文档中的所有div元素。

jQuery 代码:

$("div", xml.responseXML)

 
$( expr, context )

This function accepts a string containing a CSS or basic XPath selector which is then used to match a set of elements.

The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS or XPath), which then finds all matching elements.

By default, if no context is specified, $() looks for DOM elements within the context of the current HTML document. If you do specify a context, such as a DOM element or jQuery object, the expression will be matched against the contents of that context.

See DOM/Traversing/Selectors for the allowed CSS/XPath syntax for expressions.

Return value: jQuery
Parameters:

  • expr (String): An expression to search with
  • context (Element|jQuery): (optional) A DOM Element, Document or jQuery to use as context
 

Example:

Finds all p elements that are children of a div element.

$("div > p")

Before:

<p>one</p> <div><p>two</p></div> <p>three</p>

Result:

[ <p>two</p> ]

Example:

Searches for all inputs of type radio within the first form in the document

$("input:radio", document.forms[0])

Example:

This finds all div elements within the specified XML document.

 $("div", xml.responseXML)  
相关链接
asp之家 | jQuery官方网站 | jQuery中文网 | 电子书作者网站 | 电子书作者blog