HTML DOM getElementById() ဘက်တုံး

အသုံးပြုခြင်း နှင့် အသုံးပြုခြင်း

getElementById() ဘက်တုံး က အမှတ်အသား ကို ပေးထားသော ပထမဆုံး အမှုန့် ကို ပြန်လည်ပေးသည်။

အက္ခရာ

document.getElementById(id)

အကြောင်းကြား

HTML DOM က အမှတ်အသား ရှာရန် အမျိုးမျိုးသော ဘက်တုံး ကို အသုံးပြုသည်။ getElementById() အပြင် ကို getElementsByName() နှင့် getElementsByTagName() လည်း ပါဝင်သည်။

သို့သော်လည်း သင် အမှတ်အသား ကို ရှာရန် အများဆုံး အကျိုးဆက် ဘက် မူကြည့်တည်းမှာ ဖြစ်သည်။

အခွက် မူကြည့်တည်းမှာ အမှတ်အသား ကို ပေးပြီး အမှတ်အသား ကို မူကြည့်တည်းမှာ ကို မူကြည့်တည်းမှာ အသုံးပြုရမည်။

实例

例子 1






This is a header

Click on the header to alert its value

例子 2

getElementById() 是一个重要的方法,在 DOM 程序设计中,它的使用非常常见。我们为您定义了一个工具函数,这样您就可以通过一个较短的名字来使用 getElementById() 方法了:

function id(x) {
  if (typeof x == "string") return document.getElementById(x);
  return x;
  }

上面这个函数接受元素 ID 作为它们的参数。对于每个这样的参数,您只要在使用前编写 x = id(x) 就可以了。