Common Methods for jquery to operate dom _ jquery-js tutorial
Source: Internet
Author: User
Jquery's operations on dom include adding html content, deleting element content, and operating CSS. This article provides some common methods for dom operations, if you are interested, you can refer to the operations that Jquery has on dom.
1. Three simple and practical jQuery methods for DOM operations:
· Text ()-set or return the text content of the selected Element
· Html ()-set or return the content of the selected element (including HTML tags)
· Val ()-set or return the value of a form field
2. attr () Get attributes. Of course, these two values can also be modified by yourself,
3. Add html content. Add new HTML content
We will learn the four jQuery methods used to add new content:
· Append ()-insert content at the end of the selected Element
· Prepend ()-insert content at the beginning of the selected Element
· After ()-insert content after the selected Element
· Before ()-insert content before the selected Element
4. Delete elements/Content
To delete elements and content, you can use the following two jQuery methods:
· Remove ()-delete the selected element (and its child element)
· Empty ()-delete a child element from the selected Element
5. Operate CSS using jQuery
JQuery has several methods to perform CSS operations. We will learn the following:
· AddClass ()-add one or more classes to the selected Element
· RemoveClass ()-deletes one or more classes from the selected Element
· ToggleClass ()-Adds or deletes the selected element to/from the switch operation.
· Css ()-set or return style attributes
Example
$ ("H1, h2, p"). addClass ("blue"); first, we know that we use ',' to separate Multiple labels. Blue must be a set. Blue {} class selector
$ ("H1, h2, p"). removeClass ("blue ");
$ ("H1, h2, p"). toggleClass ("blue"); removed if there is a style, and added if there is no style
6. set multiple CSS attributes
To set multiple CSS attributes, use the following syntax:
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.