JQuery method summary
1. Access the attributes and content of HTML elements
1.1 Use jQuery to obtain jQuery objects corresponding to HTML elements
Var DOM object = document. getElementById (Object id) // write 'id' directly to the Object id, not the selector
Var jQuery object = jQuery selector. get (INDEX) // $ ('A'). get (0) get the first a tag
You can also use the each () method to traverse all matching elements of the jQuery selector and execute the specified callback function for each element.
Each (callback function)
1.2
Use jQuery to get and set the content of HTML elements
You can call the html (), text (), and val () methods to obtain and set the content of HTML elements.
...
Var value = jQuery object. val (); // get
JQuery object. val (value); // set
The val () method can also specify a function to set the content Syntax of HTML elements as follows:
$ (Selector). var (function (index, oldvalue ))
Parameters are described as follows:
$ (Selector): selector.
Index: an optional parameter. The index position of the receiving selector (when multiple elements are selected, they are valid ).
Oldvalue: an optional parameter. The current Value attribute of the receiving selector.
Example: $ ('input'). var (function (index, oldvalue ){
Return oldvalue. toUpperCase ();
});
1.3 Use jQuery to get and set attributes of HTML elements
Var a = jQuery object. attr ('Property name'); // get the property
JQuery object. Attr ('Property name', 'Property attribute'); // you can specify attributes.
You can set a set of attributes for matching elements in the form of key/value pairs.
Example: $ ('img '). attr ({
Src :'....',
Alt :'.....'
});
1.4 Use jQuery to get and set attributes of HTML elements
JQuery object. removeAtrr ('Property name ');
1.5 Use jQuery to add content to the webpage
1. The append () method can be called to append content to the HTML element:
JQuery object. append ('append content'); // It can be a tag that is added to the element;
2. insert content before the HTML Element
JQuery object. before ('append content ')
3. insert content behind the HTML Element
JQuery object. after ('append content ')
2. Use jQuery to manage HTML elements
2.1 use jQuery to traverse HTML elements
Using the jQuery selector, you can easily match and operate HTML elements that meet certain conditions. However, you can use the find () method to traverse HTML elements that meet the conditions.
Result set = find (selector)
Then, you can use the for statement to traverse the objects in the result set;
2.2 Use jQuery to check whether an element contains a specified Element
JQuery object. has (sub-element name) // you can use $ ('lil'). has ('ul ') to select all li elements containing ul elements.
2.3 use JQuery to delete HTML elements
1. Use the empty () method to delete the content of HTML elements and all child elements
JQuery object. empty ()
2. Use the remove () method to delete HTML elements
JQuery object. remove ([selector])
The selector is an optional parameter that allows you to delete matching HTML elements. If no parameter is specified, all HTML elements corresponding to the jQuery object are deleted.
2.4 insert HTML elements using jQuery
1. after ()/insertafter () insert the specified content after the selected Element
2.5 use jQuery to copy HTML elements
You can call the Clone () method to copy HTML elements.
Clone ([withDataAndEvents])
The withDataAndEvents parameter specifies whether the data and processing functions of HTML elements are also copied. The default value is false.
2.6 Use jQuery to replace HTML elements
1. replaceWith () method
JQuery object. replaceWith (replaced content );
The selector is usually used for jQuery objects. ReplaceWith () method replaces the HTML element corresponding to the jQuery object with the content in the Parameter
2. repaceAll ()
JQuery object. replaceAll (target)
JQuery objects usually represent the replaced content. Target can be a jQuery object, DOM object after selector. Specifies the HTML element to be replaced.