Set Properties -attr ()
The JQuery attr () method is also used to set/change property values.
The following example shows how to change the value of the href attribute in a link (set):
Instance
Copy Code code as follows:
$ ("button"). Click (function () {
$ ("#keleyi"). attr ("href", "http://www.jb51.net");
});
The attr () method also allows you to set multiple properties at the same time.
The following example shows how to set the href and title properties at the same time:
Instance
Copy Code code as follows:
$ ("button"). Click (function () {
$ ("#keleyi"). attr ({
"href": "Http://www.jb51.net",
"title": "Collayi Net"
});
});
callback function for attr ()
The JQuery Method attr () also provides callback functions. The callback function consists of two parameters: the subscript for the current element in the selected element list, and the original (old) value. Then return the string you want to use with the new value of the function.
The following example shows the attr () method with a callback function:
Instance
Copy Code code as follows:
$ ("button"). Click (function () {
$ ("#keleyi"). attr ("href", function (I,origvalue) {
return origvalue + "/jquery";
});
});