Home > Web Front-end > JS Tutorial > How to create, then modify, and then add DOM elements with jQuery_jquery

How to create, then modify, and then add DOM elements with jQuery_jquery

WBOY
Release: 2016-05-16 16:47:38
Original
1162 people have browsed it

How to manipulate DOM elements in one go, on the fly?

For example, execute three actions in sequence [Create]-> [Modify]-> [Add].

Since jQuery supports chain operations, which is actually the builder mode of design mode, we can string three operations together to execute them.

First create a p element whose content contains an a element.

Copy code The code is as follows:

Then add a href attribute to the a element
Copy code The code is as follows:

$('

jQuery

').find('a').attr(' href', 'http://www.jquery.com')

Finally add the newly added p element to the body
Copy the code The code is as follows:

$('

jQuery

').find(' a').attr('href', 'http://www.jquery.com').end().appendTo('body')

Note that this place needs to execute end( ) operation, otherwise the element added to the body is not a p element but becomes an a element in the p element.

Actually, the end() operation cannot be equated with cancellation. It returns the previous selection, but this selection has been modified by the operation before end.
Copy code The code is as follows:







Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template