Yes, I know this has been asked a lot. But, it confuses me, since the results on google for this search show different methods (listed below)
$(document).ready(function() {
if ($('#DivID').length){
alert('Found with Length');
}
if ($('#DivID').length > 0 ) {
alert('Found with Length bigger then Zero');
}
if ($('#DivID') != null ) {
alert('Found with Not Null');
}
});
Which one of the 3 is the correct way to check if the div exists?
EDIT: It's a pitty to see that people do not want to learn what is the better approach from the three different methods. This question is not actually on "How to check if a div exists" but it's about which method is better, and, if someone could explain, why it it better?
首先是最简洁的,我同意。前两个相同,但是第一个短一点,因此您将节省字节数。第三个是完全错误的,因为该条件永远将为true,因为该对象永远不会为null或虚假。