Set attributes-Attr ()
The jQuery attr () method is also used to set/change attribute values.
The following example shows how to change the value of the href attribute in the (SET) link:
Instance
Copy codeThe Code is as follows:
$ ("Button"). click (function (){
$ ("# Keleyi"). attr ("href", "http://www.jb51.net ");
});
The attr () method also allows you to set multiple attributes at the same time.
The following example shows how to set href and title attributes simultaneously:
Instance
Copy codeThe Code is as follows:
$ ("Button"). click (function (){
$ ("# Keleyi"). attr ({
"Href": "http://www.jb51.net ",
"Title": "Ke leyi network"
});
});
Attr () callback function
The jQuery method attr () also provides callback functions. The callback function consists of two parameters: the subscript of the current element in the list of selected elements and the original (old) value. Then return the string you want to use with the new value of the function.
The following example demonstrates the attr () method with a callback function:
Instance
Copy codeThe Code is as follows:
$ ("Button"). click (function (){
$ ("# Keleyi"). attr ("href", function (I, origValue ){
Return origValue + "/jquery ";
});
});