HTML DOM setAttribute() နည်းလမ်း
❮ Element Objectဥပမာ
"democlass" တန်ဖိုးဖြင့် အတန်းရည်ညွှန်းချက်ကို <h1> ဒြပ်စင်သို့ ထည့်ပါ-
document.getElementsByTagName("H1")[0].setAttribute("class", "democlass");
ရည်ညွှန်းချက်ကို မသတ်မှတ်မီ-
Hello World
ရည်ညွှန်းချက်ကို သတ်မှတ်ပြီးနောက်-
Hello World
အောက်တွင် နောက်ထပ် "စမ်းသုံးကြည့်ပါ" ဥပမာများ။
အဓိပ္ပါယ်နှင့် အသုံးပြုမှု
setAttribute() method သည် သတ်မှတ်ထားသော attribute ကို ဒြပ်စင်တစ်ခုသို့ ပေါင်းထည့်ကာ ၎င်းကို သတ်မှတ်ထားသော တန်ဖိုးကို ပေးသည်။
သတ်မှတ်ထားသော ရည်ညွှန်းချက်ရှိနှင့်ပြီးဖြစ်ပါက၊ တန်ဖိုးကိုသာ သတ်မှတ်/ပြောင်းလဲထားသည်။
မှတ်ချက်- ဤနည်းလမ်းဖြင့် ဒြပ်စင်တစ်ခုသို့ စတိုင်အရည်အသွေးကို တန်ဖိုးတစ်ခုဖြင့် ထည့်နိုင်သော်လည်း၊ ၎င်း တွင် သတ်မှတ်ထားနိုင်သည့် အခြားသော CSS ဂုဏ်သတ္တိများကို ထပ်ရေးမည်မဟုတ်သောကြောင့်၊ ၎င်းတွင် သတ်မှတ်ထားသော အခြား CSS ဂုဏ်သတ္တိများကို ထပ်ရေးမည်မဟုတ်ပါ။ စတိုင်အရည်အသွေး-
မကောင်း
element.setAttribute("style", "background-color: red;");
ကောင်းသည်-
element.style.backgroundColor = "red";
အကြံပြုချက်- ဒြပ်စင်တစ်ခုမှ attribute ကိုဖယ်ရှားရန် removeAttribute() နည်းလမ်းကို အသုံးပြု ပါ။
အကြံပြုချက်- setAttributeNode() နည်းလမ်း ကိုလည်း ကြည့်ပါ ။
Browser ပံ့ပိုးမှု
ဇယားရှိ နံပါတ်များသည် နည်းလမ်းကို အပြည့်အဝထောက်ခံသည့် ပထမဆုံးဘရောက်ဆာဗားရှင်းကို သတ်မှတ်ပေးသည်။
Method | |||||
---|---|---|---|---|---|
setAttribute() | Yes | 8.0 | Yes | Yes | Yes |
အထားအသို
element.setAttribute(attributename, attributevalue)
ကန့်သတ်တန်ဖိုးများ
Parameter | Type | Description |
---|---|---|
attributename | String | Required. The name of the attribute you want to add |
attributevalue | String | Required. The value of the attribute you want to add |
နည်းပညာဆိုင်ရာအသေးစိတ်
ပြန်ပို့တန်ဖိုး- | ပြန်တန်ဖိုးမရှိပါ။ |
---|---|
DOM ဗားရှင်း | Core Level 1 Element Object |
နောက်ထပ် ဥပမာများ
ဥပမာ
ထည့်သွင်းမှုအကွက်ကို အဝင်ခလုတ်တစ်ခုသို့ ပြောင်းပါ-
document.getElementsByTagName("INPUT")[0].setAttribute("type", "button");
ရည်ညွှန်းချက်ကို မသတ်မှတ်မီ-
ရည်ညွှန်းချက်ကို သတ်မှတ်ပြီးနောက်-
ဥပမာ
<a> အစိတ်အပိုင်းတစ်ခုသို့ "www.w3schools.com" တန်ဖိုးဖြင့် href attribute တစ်ခုထည့်ပါ-
document.getElementById("myAnchor").setAttribute("href", "https://www.w3schools.com");
ရည်ညွှန်းချက်ကို မသတ်မှတ်မီ-
ရည်ညွှန်းချက်ကို သတ်မှတ်ပြီးနောက်-
ဥပမာ
<a> ဒြပ်စင်တစ်ခုတွင် ပစ်မှတ်ရည်ညွှန်းချက်ရှိမရှိ ရှာဖွေပါ။ သို့ဆိုလျှင်၊ ပစ်မှတ်ရည်ညွှန်းချက်၏တန်ဖိုးကို "_self" သို့ပြောင်းပါ-
// Get the <a> element with id="myAnchor"
var x = document.getElementById("myAnchor");
// If the <a> element has a target attribute, set the value to "_self"
if (x.hasAttribute("target")) {
x.setAttribute("target", "_self");
}
ဆက်စပ်စာမျက်နှာများ
HTML သင်ခန်းစာ- HTML အရည်အချင်းများ
HTML DOM အကိုးအကား- getAttribute() နည်းလမ်း
HTML DOM ရည်ညွှန်းချက်- hasAttribute() နည်းလမ်း
HTML DOM အကိုးအကား- removeAttribute() နည်းလမ်း
❮ Element Object