Jquery-modifying HTML Elements

Source: Internet
Author: User

Adding and removing HTML Elements

Dynamically adding elements to US HTML page is a powerful tool-it lets we modify not only the formatting, but the actual structure of our websites in response to a user ' s actions.

. Append () inserts the specified element as the last child of the target element. $ (". Info"). Append ("<p>Stuff!</p>");

. Prepend () inserts the specified elements as the first child of the target element. $ (". Info"). Prepend ("<p>Stuff!</p>");

. AppendTo () does the same as. Append (), $ (' <p>stuff!</p> '). AppendTo ('. Info ');

. Prepend () does the same as. Prepend ()

We can specify where in the DOM we insert a element with the . Before ( ) and . After () functions.

$ (' target '). After (' <tag>to add</tag> ');

var // existing elements // Move It // Same as:$ ("div"). After ($ ("P"));

Note:this does not copy the element from one location to another, it moves the original element effectively saving you fr Om has to delete the original

. Empty () deletes an element's content and all its descendants.

The . Remove () is deletes an element's content, but deletes the element itself.

modifying Classes and Content

. addclass () and. Removeclass () can used to add or remove a class from an element.

$ (' selector '). addclass (' className '); $ (' selector '). Removeclass (' ClassName ');

.toggleClass()Function that is does exactly this. If the element it ' s called on Have the class it receives as an input, .toggleClass() removes that class; if the target element doesn ' t has that class, .toggleClass() adds


$("div").css("background-color","#008800"); //change css style

. HTML () and. Val can update contents of our HTML elements.

$ (' div '). html ("I love jquery!");

. val () is used to get the value of from elements.

$ (' input:checkbox:checked '). Val ();

Jquery-modifying HTML Elements

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.