HTML DOM Element setAttributeNode() method
- Nasunod na Pahina setAttribute()
- Susunod na Pahina Style
- Bumalik sa isang Lebel HTML DOM Elements Obheto
Definition and usage
setAttributeNode()
This method adds the specified attribute node to the element and returns the Attribute object.
If this specified attribute already exists, this method will replace it.
Alternative solution:
Use setAttribute() method Easier.
See also:
Reference Manual:
Tutorial:
The difference between setAttribute() and setAttributeNode()
setAttribute()
Method to replace attribute value.
setAttributeNode()
Method to replace Attribute object.
Before adding the attribute to the element, you must create an Attr object and set the Attr value.
The result will be the same.
Instance
Example 1
Set the class attribute node of the first <h1> element:
const attr = document.createAttribute("class"); attr.value = "democlass"; const h1 = document.getElementsByTagName("H1")[0]; h1.setAttributeNode(attr);
Bago ng pagtatakda:
Hello World
Pagkatapos ng pagtatakda:
Hello World
Halimbawa 2
I-set ang katangian na bagay ng href ng <a> na bagay:
const attr = document.createAttribute("href"); attr.value = ""; const anchor = document.getElementById("myAnchor"); anchor.setAttributeNode(attr);
Bago ng pagtatakda:
Bisita ang codew3c.com
Pagkatapos ng pagtatakda:
Bisita ang codew3c.com
Mga pangunahing sintaksis
element.setAttributeNode(newAttr)
Parametro
Parametro | Paglalarawan |
---|---|
newAttr | Mga kinakailangan. Naglalaman ng Attr na bagay na kailangan idagdag o ang katangian na kailangan ay baguhin. |
Bumalik sa Halimbawa
Uri | Paglalarawan |
---|---|
Bagay |
Ang Attr na bagay ng pinalitan na katangian na bagay. Kung walang katangian ang pinalitan, ito ay null. |
Tumatanong
Ang paraan na ito ay magtatanong ng kahinaan na DOMException na naglalaman ng sumusunod na code:
Kahinaan | Paglalarawan |
---|---|
INUSE_ATTRIBUTE_ERR | newAttr Ito ay miyembro ng pangkat ng katangian ng ibang Element na nito. |
NO_MODIFICATION_ALLOWED_ERR | Ang kasalukuyang Element na nito ay readonly, hindi pinapayagan ang pagbabago ng kanyang mga katangian. |
WRONG_DOCUMENT_ERR | newAttr Ang ownerDocument na katangian ay iba sa Element na nais ipaseta. |
Suporta ng Browser
element.setAttributeNode()
Ito ay DOM Level 1 (1998) na katangian.
Lahat ng mga browser ay ganap na sumusuporta sa ito:
Chrome | IE | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
Chrome | IE | Edge | Firefox | Safari | Opera |
Suporta | 9-11 | Suporta | Suporta | Suporta | Suporta |
- Nasunod na Pahina setAttribute()
- Susunod na Pahina Style
- Bumalik sa isang Lebel HTML DOM Elements Obheto