jQuery API | 返回首页目录 | jQuery API 中英文对照版 |
$(expr,context) |
$(expr, context)
这个函数接收一个包含CSS或基本的XPath选择符的字符串,然后用这个字符串去匹配一组元素。 返回值: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) 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
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) |