[JavaScript][jQuery] Checking for the existence of DOM elements
There are many ways to check for the existence of DOM elements in JavaScript, which can be confusing, but the conclusion is that
document.getElementById("id") != null
seems to be the fastest and best for checking.
コード 速度 document.getElementById("id") != null
0.019ms $("selector")[0]
0.033ms $("selector").get(0)
0.040ms $("selector").size()
0.041ms $("selector").length
0.069ms $("selector").is("*")
0.169ms
That’s all from the Gemba.