I. jQuery core functions
First, we will introduce several core functions of jQuery, which play a vital role in jQuery. They are also the most used in the actual front-end development process.
1. jQuery (elements)
Converts one or more DOM elements to jQuery objects.
This function can also receive XML documents and Window objects (although they are not DOM elements) as valid parameters.
Returned value: jQuery object
Parameters
Elements: DOM elements used to encapsulate jQuery objects
// Set the background color of the front page to black jquery(document.body).css ("background-color", "black ");
Let's look at the following code:
// Set the background color of the front page to black background (document.body).css ("background-color", "black ");
The code can also run normally and the execution result is the same as the preceding code. What is the relationship between $ and the previous jQuery identifier?
Open jQuery's source file jquery-1.3.2.js and find the following code
JQuery = window. jQuery = window. $ = function (selector, context) {// The jQuery object is actually just the init constructor 'enabled' return new jQuery. fn. init (selector, context );}
$ And jQuery represent the same function in the jQuery framework. The effects of jQuery and $ are the same; in fact, $ is used in the jQuery framework as an alias for the jQuery method (easier to use). Due to concerns about $ conflicts with $ identifiers in other javascript frameworks (such as asp.net ajax) and even in personal code, you can also use jQuery. the noConflict () function removes the association between $ and the jQuery function, so $ does not represent the jQuery method, but the original $ object.
2. jQuery (expression, [context])
This function receives a string containing the CSS selector and uses this string to match a group of elements.
JQuery's core functions are implemented through this function. Everything in jQuery is based on this function, or is using this function in some way. The most basic usage of this function is to pass an expression (usually composed of CSS selectors) to it, and then find all matching elements based on this expression.
Returned value: jQuery object
Parameters
Expression: string used to search for DOM elements
Context: (optional) serves as the DOM Element Set, document, or jQuery object to be searched. It is used to limit the search range.
// Search for element A under all DIV elements $ ("div> ");
3. jQuery (html)
Dynamically creates DOM elements encapsulated by jQuery Objects Based on HTML strings.
Returned value: jQuery object
Parameters
Html: HTML string used to create DOM elements
// Add a jquery.com hyperlink to the page body element $ ("<a href = http://jquery.com/> jquery.com </a>"). appendTo ("body ");
Ii. jQuery object
The Return Value of the jQuery core function is a jQuery object. By performing operations on the jQuery object, most tasks in javascript programming can be completed. What is the jQuery object?
You can retrieve a DOM object by using doc [0] And doc [1]. Others are the attributes and Methods unique to jQuery objects. In fact, jQuery objects encapsulate DOM objects, it also contains some jQuery methods that operate DOM elements.
In the process of using jQuery, the first step is also the most important work in most cases, that is, to obtain the jQuery object that wraps the DOM object; then, you can call the method of the obtained jQuery object to perform operations on the DOM object.