$('#idThatDoesnotexist').length
is what you're looking for. (If it finds nothing, this will === 0
.) So your conditional statement should probably be:
if($('#id').length) { /* code if found */ } else { /* code if not found */ }
You're getting an object returned from that alert because jQuery (almost) always returns the "jQuery object" when you use it, which is a wrapper for the elements jQuery's found that permits method chaining.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…