这个jquery $(' .some-class',$('#some-id'))是什么意思?

时间:2012-12-11 06:48:21

标签: javascript jquery html syntax jquery-selectors

我知道$('.some-class')$('#some-id')的含义,但我真的不知道$('.some-class',$('#some-id'))的含义,希望有人可以为我解释,非常感谢。

3 个答案:

答案 0 :(得分:4)

selector contextsome-class将在元素中使用ID为some-id的元素进行查找。

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

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

Without context $('。some-class')将带有类some-class的文档中的所有元素。 With context $('。some-class',$('#some-id'))将带有ID为some-id的元素中的所有元素。

答案 1 :(得分: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 );
    },

答案 2 :(得分:1)

您在.some-class中搜索#some-id