Metoda jQuery attr() .

❮ jQuery HTML/CSS metody

Příklad

Nastavte atribut width obrázku:

$("button").click(function(){
  $("img").attr("width","500");
});

Definice a použití

Metoda attr() nastavuje nebo vrací atributy a hodnoty vybraných prvků.

Když se tato metoda použije k vrácení hodnoty atributu, vrátí hodnotu PRVNÍHO shodného prvku.

Když se tato metoda použije k nastavení hodnot atributů, nastaví jeden nebo více párů atribut/hodnota pro sadu odpovídajících prvků.


Syntax

Vrátí hodnotu atributu:

$(selector).attr(attribute)

Nastavte atribut a hodnotu:

$(selector).attr(attribute,value)

Nastavte atribut a hodnotu pomocí funkce:

$(selector).attr(attribute,function(index,currentvalue))

Nastavte více atributů a hodnot:

$(selector).attr({attribute:value, attribute:value,...})

Parameter Description
attribute Specifies the name of the attribute
value Specifies the value of the attribute
function(index,currentvalue) Specifies a function that returns the attribute value to set
  • index - Receives the index position of the element in the set
  • currentvalue - Receives the current attribute value of selected elements

Zkuste si to sami - příklady


Jak vrátit hodnotu atributu pro prvek.


Jak používat funkci k nastavení hodnoty atributu pro prvek.


Jak nastavit více atributů/hodnot pro prvek.


❮ jQuery HTML/CSS metody