HTML DOM Document createAttribute() పద్ధతి

నిర్వచనం మరియు ఉపయోగం

code>createAttribute() పద్ధతి అంశాన్ని సృష్టించి ఆ అంశాన్ని Attr ఆబ్జెక్ట్ గా తిరిగి ఇస్తుంది.ప్రత్యామ్నాయ సంధానం:

ఉపయోగించండి: setAttribute() పద్ధతి సులభం.

మరింత చూడండి:

Element setAttribute() పద్ధతి

Attribute value పద్ధతి

Element setAttributeNode() పద్ధతి

ప్రతిమాత్రము

ఉదాహరణ 1

// class అట్రిబ్యూట్ సృష్టించండి:
const att = document.createAttribute("class");
// class అట్రిబ్యూట్ యొక్క విలువ అమర్చండి:
att.value = "democlass";
// మొదటి h1లో class అట్రిబ్యూట్ జోడించండి:
const h1 = document.getElementsByTagName("H1")[0];
h1.setAttributeNode(att);

స్వయంగా ప్రయత్నించండి

ఉదాహరణ 2

// style అట్రిబ్యూట్ సృష్టించండి:
const att = document.createAttribute("style");
// style అట్రిబ్యూట్ యొక్క విలువ అమర్చండి:
att.value = "color:red";
// మొదటి h1కు style అట్రిబ్యూట్ జోడించండి:
const h1 = document.getElementsByTagName("h1")[0];
h1.setAttributeNode(att);

స్వయంగా ప్రయత్నించండి

ఉదాహరణ 3

అంకర్ ఎలిమెంట్కు href="www.codew3c.com" అట్రిబ్యూట్ జోడించండి:

// href అట్రిబ్యూట్ సృష్టించండి:
const att = document.createAttribute("href");
// href అట్రిబ్యూట్ యొక్క విలువ అమర్చండి:
att.value = "https://www.codew3c.com";
// href అట్రిబ్యూట్ ను అందుకు జోడించండి:
element.setAttributeNode(att);

స్వయంగా ప్రయత్నించండి

సింథెక్స్

document.createAttribute(name)

పారామీటర్లు

పారామీటర్లు వివరణ
name అవసరం. సృష్టించాలి అట్రిబ్యూట్ పేరు.

వాయిదా అనుబంధం

రకం వివరణ
నోడ్ కొత్తగా సృష్టించబడిన అట్రిబ్యూట్ నోడ్. nadeName అట్రిబ్యూట్ అమర్చబడింది: name

అపఘాతం చేస్తుంది

ఇది name అనేది విషయం లేని అక్షరాలను కలిగి ఉంటే, ఈ పద్ధతి కోడ్ గా అపఘాతం చేస్తుంది: INVALID_CHARACTER_ERR యొక్క DOMException అపఘాతం.

బ్రౌజర్ మద్దతు

document.createAttribute() ఇది DOM Level 1 (1998) లక్షణం ఉంది。

అన్ని బ్రౌజర్లు దానిని మద్దతు ఇస్తాయి:

క్రోమ్ ఐఇ ఎడ్జ్ ఫైర్ఫాక్స్ సఫారీ ఆపెరా
క్రోమ్ ఐఇ ఎడ్జ్ ఫైర్ఫాక్స్ సఫారీ ఆపెరా
మద్దతు 9-11 మద్దతు మద్దతు మద్దతు మద్దతు