(3) jquery-manipulating the DOM with jquery

Source: Internet
Author: User

1. Get the attributes of an element

Gets and sets properties using the Attr method of jquery, removing the attribute using the Removeattr method.

Syntax: $ (selector). attr (attribute)

Here's an example: $ (' img '). attr (' src ')//Get the SRC attribute under the IMG element

2. Setting the attributes of an element

Since we can all get the attributes of the element, we will generally operate on the acquired element.

Syntax: $ (selector). attr (Attribute,value)

For example, we can set a div title, id, and so on.

// set the div's ID $ (' #div1 '). attr (' id ', ' div2 ');

Of course, the interesting thing about jquery, of course, is that it supports setting multiple properties.

// set multiple properties $ (' #div1 '). attr ({     ' href ': ' http://www.baidu.com ',     ' title ': ' Baidu ' });

3. Modifying element content

There are 3 ways to get the content of an element:

(1) text (): Sets or returns the text content of the selected element

(2) HTML (): Sets or returns the contents of the selected element (including HTML tags)

(3) Val (): Sets or returns the value of a form field

The biggest difference between text and HTML is that text returns only the textual content of the element, and HTML returns the parsed content of the HTML.

Val returns the contents of the form.

4. Dynamically Create Content

Syntax: $ (HTML)

Remember the first jquery you started with? That's the dynamically created Div.

(3) jquery-manipulating the DOM with jquery

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.