2

我知道和的意思$('.some-class')$('#some-id')但我真的不知道的意思$('.some-class',$('#some-id')),希望有人能解释一下,非常感谢。

4

3 回答 3

4

您拥有selectorwith contextsome-class将在带有 id 的元素中查找元素some-id

'.some-class'是选择器并且$('#some-id')是上下文

选择器的 jQuery 文档中的语法是您可以在此处jQuery( selector [ , context ] )阅读有关选择器的更多信息

Without context$('.some-class') 将把文档中的所有元素都带入类 some-class。With context$('.some-class', $('#some-id')) 会将所有带有 in 元素的元素带入 id some-id

于 2012-12-11T06:49:56.333 回答
3

第二个参数是上下文

如果您查看 jQuery 源代码,您可以将其视为 $ 或 jQuery 函数的第二个参数。

$('selector')遍历整个文档

$('selector',context)在给定的上下文/元素中遍历

jQuery库源代码的几行

(function( window, undefined ) {

// Define a local copy of jQuery
var jQuery = function( selector, context ) {
        /// <summary>
        ///     1: $(expression, context) - This function accepts a string containing a CSS selector which is then used to match a set of elements.
        ///     2: $(html) - Create DOM elements on-the-fly from the provided String of raw HTML.
        ///     3: $(elements) - Wrap jQuery functionality around a single or multiple DOM Element(s).
        ///     4: $(callback) - A shorthand for $(document).ready().
        ///     5: $() - As of jQuery 1.4, if you pass no arguments in to the jQuery() method, an empty jQuery set will be returned.
        /// </summary>
        /// <param name="selector" type="String">
        ///     1: expression - An expression to search with.
        ///     2: html - A string of HTML to create on the fly.
        ///     3: elements - DOM element(s) to be encapsulated by a jQuery object.
        ///     4: callback - The function to execute when the DOM is ready.
        /// </param>
        /// <param name="context" type="jQuery">
        ///     1: context - A DOM Element, Document or jQuery to use as context.
        /// </param>
        /// <returns type="jQuery" />

        // The jQuery object is actually just the init constructor 'enhanced'
        return new jQuery.fn.init( selector, context );
    },
于 2012-12-11T06:51:02.263 回答
1

.some-class#some-id.

于 2012-12-11T06:50:12.563 回答