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" ਅਟਰੀਬਿਊਟ ਨੂੰ anchor ਐਲੀਮੈਂਟ ਵਿੱਚ ਜੋੜੋ:

// 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) ਵਿਸ਼ੇਸ਼ਤਾ ਹੈ。

ਸਾਰੇ ਬਰਾਊਜ਼ਰ ਇਸ ਨੂੰ ਸਮਰਥਨ ਕਰਦੇ ਹਨ:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
ਸਮਰਥਨ 9-11 ਸਮਰਥਨ ਸਮਰਥਨ ਸਮਰਥਨ ਸਮਰਥਨ