Home > Article > Web Front-end > jquery learning 2 attribute related_jquery
attr(name)
Get the attribute value of the first matching element. This method makes it easy to get the value of an attribute from the first matching element. If the element has no corresponding attribute, 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
--------------------------------------- -------------------------------------------------- -----------------------------------------------
attr (properties)
Sets a name/value object as a property of all matching elements.
This is the best way to set many properties in bulk across all matching elements. Note that if you want to set the class attribute of an object, you must use 'className' as the attribute name. Or you can use .addClass( class ) and .removeClass( class ) directly.
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): Name/value pair objects as properties
Example
Set the src and alt attributes for all images.
HTML code:
jQuery code:
$("img").attr({ src: "test.jpg", alt: "Test Image" });
Result:
[ ]
------------------------ -------------------------------------------------- -------------------------------------------------- ----------
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): Property name
value ( Object): Attribute value
Example
Set the src attribute for all images.
HTML code:
jQuery code:
$("img").attr("src"," test.jpg");
Result:
[ , ]
--------------------- -------------------------------------------------- -------------------------------------------------- ----------------
attr(key,fn)
Set a calculated attribute value for all matching elements.
Does not provide a value, but provides a function, and 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 'above', a function is provided that computes the value.
Return value
jQuery
Parameter
key (String): Attribute name
fn (Function): Function scope of the return value: current element, Parameter: Index value of the current element
Example
sets the value of the src attribute to the value of the title attribute.
HTML code:
jQuery code:
$("img").attr("title", function() { return this. src });
Result:
-------------------------- -------------------------------------------------- -------------------------------------------------- ------
removeAttr(name)
Remove an attribute from each of the matched elements.
Return value
jQuery
Parameter
name (String): the attribute name to be deleted
Example
Delete the src attribute of the image in the document
HTML code:
jQuery code:
$("img").removeAttr("src");
Result:
[
]