Metoda setAttributeNode() HTML DOM
❮ Objekt prvkuPříklad
Nastavte uzel atributu třídy prvku <h1>:
var h1 = document.getElementsByTagName("H1")[0]; // Get the first <h1> element in the document
var att = document.createAttribute("class"); // Create a "class" attribute
att.value = "democlass"; // Set the value of the class attribute
h1.setAttributeNode(att); // Add the class attribute to <h1>
Před nastavením uzlu atributu:
Hello World
Po nastavení uzlu atributu:
Hello World
Další příklady „Vyzkoušejte si to sami“ níže.
Definice a použití
Metoda setAttributeNode() přidá zadaný uzel atributu k prvku.
Pokud zadaný atribut již existuje, tato metoda jej nahradí.
Návratovou hodnotou této metody je objekt Attr. Další informace naleznete v tématu Objekt atributu HTML DOM .
Viz také metoda setAttribute() .
Tip: Pomocí metody removeAttributeNode() odeberte uzel atributu z prvku.
Podpora prohlížeče
Method | |||||
---|---|---|---|---|---|
setAttributeNode() | Yes | Yes | Yes | Yes | Yes |
Syntax
element.setAttributeNode(attributenode)
Hodnoty parametrů
Parameter | Type | Description |
---|---|---|
attributenode | Attr object | Required. The attribute node you want to add |
Technické údaje
Návratová hodnota: | Objekt Attr představující nahrazený uzel atributu, pokud existuje, jinak má hodnotu null |
---|---|
Verze DOM | Základní objekt prvku úrovně 1 |
Další příklady
Příklad
Nastavte uzel atributu href prvku <a>:
var anchor = document.getElementById("myAnchor"); // Get the <a> element with id="myAnchor"
var att = document.createAttribute("href"); // Create a "href" attribute
att.value = "https://www.w3schools.com"; // Set the value of the href attribute
anchor.setAttributeNode(att); // Add the href attribute to <a>
Před nastavením uzlu atributu:
Go to w3schools.com
Po nastavení uzlu atributu:
Související stránky
HTML kurz: HTML atributy
Odkaz HTML DOM: Objekt atributů HTML DOM
Odkaz HTML DOM: metoda setAttribute().
Odkaz HTML DOM: metoda document.createAttribute() .
HTML DOM Reference: atribut .value Property
Odkaz HTML DOM: metoda getAttributeNode() .
Reference HTML DOM: metoda removeAttributeNode().
❮ Objekt prvku