JQuery--How to "create, modify, add" DOM elements first

Source: Internet
Author: User

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

For example, the "create", "Modify", "add" three actions are executed sequentially.


Since jquery supports chained operations, it is actually the builder mode of the design pattern, so we can string together three operations to execute.

First, you create a P element that contains a element of a.

$ (' <p><a>jQuery</a></p> ')


Then add an href attribute to the A element

$ (' <p><a>jQuery</a></p> ').find (' a '). attr (' href ', ' http://www.jquery.com ')


Finally add the newly added P element to the body

$ (' <p><a>jQuery</a></p> '). Find (' a '). attr (' href ', ' http://www.jquery.com '). End (). AppendTo (' body ')


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


In fact, the end () operation cannot be equivalent to undo, it returns previous selection, but the selection has been modified by the operation before the end.


<! DOCTYPE html>

Page code


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.