[JavaScript][jQuery] 要素 DOM の存在確認・チェック

Tue, January 15, 2013 - 2 min read

JavaScriptで、要素 DOM の存在確認をする方法がたくさんあるので迷うけど、結論としては、

document.getElementById("id") != null

でチェックのが速くて良いみたい。

コード速度
document.getElementById("id") != null0.019ms
$("selector")[0]0.033ms
$("selector").get(0)0.040ms
$("selector").size()0.041ms
$("selector").length0.069ms
$("selector").is("*")0.169ms

・引用元:[JS][jQuery] 要素の存在を確認する6通りのコードと実行速度 | きほんのき はてなブックマーク - [JS][jQuery] 要素の存在を確認する6通りのコードと実行速度 | きほんのき

jQueryによる要素の存在チェックまとめ: 小粋空間 はてなブックマーク - jQueryによる要素の存在チェックまとめ: 小粋空間