Home  >  Article  >  Web Front-end  >  Detailed explanation of jQuery using element attribute attr assignment_jquery

Detailed explanation of jQuery using element attribute attr assignment_jquery

WBOY
WBOYOriginal
2016-05-16 16:12:551067browse

Copy code The code is as follows:

1.$("Element").attr(name) 'Get the first matching attribute value, such as $("img").attr("src")
2.$("Element".attr(key,value)") 'Set attributes for a certain element
3.$("Element".attr({key:value,key1:value,....})) 'Set multiple attributes for an element at once
4.$("Element").attr(key,function) 'Set a calculated attribute value for all matching elements.
5.$("Element").removeAttr(name)//Remove an attribute

jQuery code:

Copy code The code is as follows:

$("img").attr("src");

Parameter properties description:

Set src and alt attributes for all images.

jQuery code:

Copy code The code is as follows:

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

Parameter key, value description:

Set the src attribute for all images.

jQuery code:

Copy code The code is as follows:

$("img").attr("src","test.jpg");

Parameter key, callback function description:

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

jQuery code:

Copy code The code is as follows:

$("img").attr("title", function() { return this.src });

The above is the entire content of this article. It is a summary of some personal experiences. I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn