what does this jquery $('.some-class',$('#some-id')) mean?

感情迁移 提交于 2020-01-04 14:04:26

问题


i know the meaning of $('.some-class') and $('#some-id'), but i do really not know the meaning of $('.some-class',$('#some-id')), hoping some one can explain it for me, many thanks.


回答1:


You have selector with context, some-class will be looked up in element with in element with id some-id.

'.some-class' is selector and $('#some-id') is context

The syntax in the jQuery documents for selector is jQuery( selector [ , context ] ) you can read more about selectors here

Without context $('.some-class') will bring all the elements in document with class some-class. With context $('.some-class', $('#some-id')) will bring all the elements with in element with id some-id.




回答2:


The second parameter is context.

If you look at jQuery source you can see it as second parameter to $ or jQuery function.

$('selector') traverse the whole document

$('selector',context) traverse with in the given context/ element

A few line from jQuery library source

(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:


You search .some-class in #some-id.



来源:https://stackoverflow.com/questions/13815037/what-does-this-jquery-some-class-some-id-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!