jquery Note Three--text/html/val/attr/prop

Source: Internet
Author: User

1. Access to Content

Three simple and practical jQuery methods for DOM manipulation:

    • Text ()-Sets or returns the text content of the selected element
    • HTML ()-Sets or returns the contents of the selected element (including HTML tags)
    • Val ()-Sets or returns the value of a form field
<PID= "Test">This is some<b>Bold</b>text in a paragraph.</P><P>Name:<inputtype= "text"ID= "Test2"value= "Mickey Mouse"></P>

$ ("#test"). Text ()------This was some bold text in a paragraph.

$ ("#test"). HTML ()-----This was some <b>bold</b> text in a paragraph.

$ ("#test2"). Val ()------Mickey Mouse

2. Get Properties

The JQuery attr () method is used to get the property value

< P ><  href= "http://www.w3cschool.cc"  ID= "w3s">w3cschool.cc </ a ></ P >

$ ("#w3s"). attr ("href")------http://www.w3cschool.cc

3. Setting content and properties

or the text, HTML, Val, attr above.

attr set multiple properties at the same time:

$ ("#w3s"). attr ({  "href": "Http://www.w3cschool.cc/jquery",  "title": "W3Schools jquery Tutorial"});

The difference between 4.attr and prop

Compared to the Attr,prop is 1.6.1 only new, both from the Chinese meaning understanding, are to get/set the property of the method (attributes and properties). Only the. attr () method used in window or document does not work properly before jQuery1.6 because the window and document cannot have attributes. Prop was born.

Reference: http://www.javascript100.com/?p=877

It is obviously better to use the Prop method when encountering properties to get or set checked,selected,readonly and disabled

The attr method inside, the most critical of the two lines of code, Elem.setattribute (name, Value + "") and ret = Elem.getattribute (name), is clearly seen, using the DOM API The SetAttribute and GetAttribute methods manipulate the property element nodes .
And the prop method inside, the most critical two lines of code, return (elem[name] = value) and return elem[name], you can understand so document.getElementById (EL) [Name] = value, which is a property that translates into a JS object.

jquery Note Three--text/html/val/attr/prop

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.