【问题标题】:what does this jquery $('.some-class',$('#some-id')) mean?这个 jquery $('.some-class',$('#some-id')) 是什么意思?
【发布时间】:2012-12-11 06:48:21
【问题描述】:

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

【问题讨论】:

  • 一种令人困惑的写作方式$('#some-id .some-class')

标签: javascript jquery html syntax jquery-selectors


【解决方案1】:

您有selectorcontextsome-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 的元素。

【讨论】:

    【解决方案2】:

    第二个参数是上下文

    如果您查看 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 );
        },
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-17
        • 2013-01-02
        • 2012-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多