Juqery Study Notes (2) DOM operations

Source: Internet
Author: User

1. Add nodes

 $ (Selector). append ()

Insert the dom element specified by the parameter into the element selected by selector, and insert the element at the end. The parameter can be a direct HTML string or a DOM element created by the $ () function.

For example, $ (selector). append ("<B> Hello </B> ");

Or $ dom =$ ("<B> Hello </B>"); $ (selector). append ($ dom );

$ (HTML). appendTo (selector)

Insert the DOM element created by $ (HTML) into the element selected by selector. The insertion position is at the end.

$ (Selector). prepend ()

Insert the dom element specified by the parameter into the element selected by selector, and insert the element at the beginning. The parameter can be a direct HTML string or a DOM element created by the $ () function.

For example: $ (selector). prepend ("<B> Hello </B> ");

Or $ dom =$ ("<B> Hello </B>"); $ (selector). prepend ($ dom );

 

$ (HTML). prependTo (selector)

Insert the DOM element created by $ (HTML) into the element selected by selector. The insertion position is at the beginning.

 

$ (Selector). after ()

Insert the dom element specified by the parameter after the element selected by the selector (instead of inside). The parameter can be a direct HTML string or a DOM element created by the $ () function.

For example, $ (selector). after ("<B> Hello </B> ");

Or $ dom =$ ("<B> Hello </B>"); $ (selector). after ($ dom );

 

$ (HTML). insertAfter (selector)

Insert the DOM element created by $ (HTML) after the element selected by selector

 

$ (Selector). before ()

Insert the dom element specified by the parameter before (rather than inside) the element selected by the selector. The parameter can be a direct HTML string or a DOM element created by the $ () function.

For example, $ (selector). before ("<B> Hello </B> ");

Or $ dom =$ ("<B> Hello </B>"); $ (selector). before ($ dom );

 

$ (HTML). insertBefore (selector)

Insert the DOM element created by $ (HTML) before the element selected by selector.

Bytes -----------------------------------------------------------------------------------------

Ii. delete a node

$ (Selector). remove ()

Delete the selected selector element. You can use another selector as the parameter in remove () to filter the elements to be deleted again.

Bytes ----------------------------------------------------------------------------------------

 

Iii. Clear nodes

$ (Selector). empty ()

Clears all content in the selected selector element.

 

Bytes -------------------------------------------------------------------------------------------

 

4. Copy nodes

$ (Selector). clone ()

Copy the selected selector element. If the event registered on this element also needs to be copied, use$ (Selector). clone (true)

Bytes --------------------------------------------------------------------------------------------

V. Replacing nodes

$ (Selector). replaceWith (HTML)

Replace the element selected by selector with the specified HTML or DOM element created by $ ().

 $ (HTML). replaceAll (selector)

Replace the element selected by selector with the element created by $ (HTML ).

Bytes -----------------------------------------------------------------------------------------------------

 

6. Package nodes

$ (Selector). wrap (HTML)

Wrap the selected selector element with the node created in HTML

For example, $ ("p"). wrap ("<div> </div>") returns <div> <p> </div>

$ (Selector). wrapInner (HTML)

Wrap the child element of the selected selector element with the node created in HTML

Bytes -------------------------------------------------------------------------------------------------------------

 

VII. Get or set node attributes

$ (Selector). attr ("attribute ")

Gets the attribute of the selected element of selector.

$ (Selector). attr ("attribute", "value ")

Set attribute of the element selected by selector to value

$ (Selector). removeAttr ("attribute ")

Remove the attribute of the element selected by selector

 

--------------------------------------------------------------------------------

 

8. style operations

$ (Selector). addClass ("classname ")

Append a calss style based on the existing element selected by selector.

$ (Selector). removeClass ("classname ")

Remove the classname style from the element selected by selector. If you want to remove all class styles on the element, use$ (Selector). removeClass ()

$ (Selector). toggleClass ("classname ")

The classname style is added or deleted alternately on the element selected by selector.

$ (Selector). hasClass ("classname ")

Determines whether the selector selected element has a classname style. If yes, true is returned. Otherwise, false is returned.

 

Bytes -----------------------------------------------------------------------------------------------

9. set or obtain HTML and text values

Optional (selector).html ()

Gets the innerHTML value of the element selected by selector.

Optional (selector).html ("HTML ")

Set the innerHTML value of the element selected by selector to HTML

 

$ (Selector). text ()

Obtains the innerText/contentText value of the selected element of selector.

$ (Selector). text ("TEXT ")

Set the innerText/contentText value of the element selected by selector to TEXT

 

$ (Selector). val ()

Obtain the value of the selected selector element (generally a text box, a drop-down list, or a single vertex ).

$ (Selector). val ("value ")

Set the value of the element selected by selector to value.

 

---------------------------------------------------------------

10. Traverse nodes

 

$ (Selector). children ()

Obtains the set of child elements selected by selector. Note that this method does not take child elements into account.

$ (Selector). next ()

Gets the next sibling node of the element selected by selector.

$ (Selector). prev ()

Obtains the previous sibling node of the selected selector element.

$ (Selector). siblings ()

Obtains all the sibling nodes before and after the selected element of the selector.

$ (Selector). closet ("html Tag ")

Example: $ (selector ). closet ("li"). If li matches selector, the selector is returned. Otherwise, the selector's parent level is queried step by step until a matched element is found. If no matched element is found, an empty jquery object is returned.

......

 

------------------------------------------------------------------------

11. CSS operations

 

((Selector).css ("style ")

Obtain the style of the element selected by selector. Both inline and external styles can be obtained. Note that the style is best written in the camper format. The same applies to the following.

((Selector).css ("style", "value ")

Sets the css style of the element selected by selector.

((Selector).css ({"style1": "value1", "style2": "value2 ",......})

Set multiple css styles

 You can use css (opacity [, value]) to obtain and set the transparency. The range is 0 ~ 1

$ (Selector). height ()

Returns the height of the selector, in PX.

$ (Selector). height (value)

Sets the selector height. If the unit is not set, the default value is PX. You can also set the unit.

Width has the same method.

 

$ (Selector). offset ()

Obtain the offsetLeft and offsetTop values of selector. The result is two values, $ (selector ). offset (). left, $ (selector ). offset (). top to reference these two values respectively

$ (Selector). position ()

Obtain the left and top values of selector compared with the last left and top values with dynamic positioning. The result has two values. The reference method is the same as above.

$ (Selector). scrollTop ()

Obtain the scrollTop value of selector.

$ (Selector). scrollTop (value)

Sets the scrollTop value of selector.

 

ScrollLeft has the same method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.