Jquery learning 2 attributes

Source: Internet
Author: User

Attr (name)
Obtains the attribute value of the First Matching Element. This method can be used to conveniently obtain the value of an attribute from the First Matching Element. If the element does not have a property, undefined is returned.
Access a property on the first matched element. this method makes it easy to retrieve a property value from the first matched element. if the element does not have an attribute with such a name, undefined is returned.
Return Value
Object

Parameters
Name (String): attribute name

Example
Returns the src attribute value of the first image in the document.

HTML code:


JQuery code:

$ ("Img"). attr ("src ");
Result:

Test.jpg
Certificate ---------------------------------------------------------------------------------------------------------------------------------------
Attr (properties)
Set an object in the form of "name/value" to the attributes of all matching elements.
This is the best way to set multiple attributes in batches among all matching elements. Note: To set the class attribute of an object, you must use 'classname' as the attribute name. Or you can directly use. addClass (class) and. removeClass (class ).
Set a key/value object as properties to all matched elements.
This serves as the best way to set a large number of properties on all matched elements. note that you must use 'classname' as key if you want to set the class-Attribute. or use. addClass (class) or. removeClass (class ).
Return Value
JQuery

Parameters
Properties (Map): The "name/value pair" Object of the attribute.

Example
Set the src and alt attributes for all images.

HTML code:


JQuery code:

$ ("Img"). attr ({src: "test.jpg", alt: "Test Image "});
Result:

[]
Certificate ---------------------------------------------------------------------------------------------------------------------------------------
Attr (key, value)
Set an attribute value for all matching elements.
Set a single property to a value, on all matched elements.
Return Value
JQuery

Parameters
Key (String): attribute name

Value (Object): Attribute value

Example
Set the src attribute for all images.

HTML code:



JQuery code:

$ ("Img"). attr ("src", "test.jpg ");
Result:

[, ]
Certificate ---------------------------------------------------------------------------------------------------------------------------------------
Attr (key, fn)
Set a calculated attribute value for all matching elements.
Instead of providing a value, a function is provided. The value calculated by this function is used as the attribute value.
Set a single property to a computed value, on all matched elements.
Instead of supplying a string value as described 'ababove ', a function is provided that computes the value.
Return Value
JQuery

Parameters
Key (String): attribute name

Fn (Function): Return Value Function range: Current element, parameter: index value of current element

Example
Set the src attribute value to the title attribute value.

HTML code:


JQuery code:

$ ("Img"). attr ("title", function () {return this. src });
Result:


Certificate ---------------------------------------------------------------------------------------------------------------------------------------
RemoveAttr (name)
Deletes an attribute from each matching element.
Remove an attribute from each of the matched elements.
Return Value
JQuery

Parameters
Name (String): name of the attribute to be deleted

Example
Delete the src attribute of the image in the document

HTML code:


JQuery code:

$ ("Img"). removeAttr ("src ");
Result:

[]

Related Article

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.