LoginSignup
1
0

HTML: DOM取得のなまくら術

Last updated at Posted at 2024-03-28

対応できるかどうかはBrowser次第。いつの間にやら非推奨になってしまったり、廃れていったりする可能性もあります

関数 なまくら術
document.getElementById,
document.querySelector
id属性値を直指し
document.getElementsByTagName("*"),
document.querySelectorAll("*")
document.all
document.getElementsByTagName("form") document.forms
document.querySelectorAll("a[href]") document.links
document.querySelectorAll("a[name]") document.anchors
document.getElementsByTagName("html")[0] document.children[0], document.lastChild, document.documentElement
document.getElementsByTagName("head")[0] document.head
document.getElementsByTagName("body")[0] document.body
document.getElementsByTagName("script") document.scripts
document.getElementsByTagName("img") document.images
document.getElementsByTagName("embed") document.embeds, document.plugins
document.getElementsByTagName("iframe")[0].contentWindow frames[0]

例題

<form id=f><input id=i></form>

上記のようなhtmlでform要素やinput要素を取得する場合、以下のようにできます

//form
a=document.forms[0];
b=document.forms.f;
c=f;
//input
A=document.forms[0].i;
B=document.forms.f.i;
C=i;

後書き

まあ当たり前尽くしの事ではありますが、過剰なまでの関数連呼やjqueryの無駄遣いをちらほら見掛けるのもまた事実

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0