.attr()
method is a convenient and powerful way to set the value of attributes—especially when setting multiple attributes or using values returned by a function. Let's consider the following image:<img id="greatphoto" src="brush-seller.jpg" alt="brush seller" />
Setting a simple attribute
We can change thealt
attribute by simply passing the name of the attribute and its new value to the .attr()
method:$('#greatphoto').attr('alt', 'Beijing Brush Seller');We can add an attribute the same way:
$('#greatphoto') .attr('title', 'Photo by Kelly Clark');
Setting several attributes at once
To change thealt
attribute and add the title
attribute at the same time, we can pass both sets of names and values into the method at once using a map (JavaScript object literal). Each key-value pair in the map adds or modifies an attribute:$('#greatphoto').attr({ alt: 'Beijing Brush Seller', title: 'photo by Kelly Clark' });When setting multiple attributes, the quotes around attribute names are optional.
WARNING When setting the 'class' attribute, you must always use quotes!
fuente: http://api.jquery.com/attr/
No hay comentarios. :
Publicar un comentario
Sentite libre de comentar, criticar y/o aportar tu granito en este (proyecto de..) mar de conocimiento ;)