javascript :
if(document.getElementById("target_obj_id")){ } else { }
jquery:
因為 $(“#target_obj_id”) 不管對象是否存在都會返回 object ,所以以上方法不行
1. var target_obj = jQuery('#target_obj_id'); if (target_obj.length > 0) { //如果大於0 標識 id 為target_obj_id的對象存在,否則不存在 } else { } 2、 if (target_obj[0]) { } else { } 3. $.isEmptyObject({}) // true $.isEmptyObject({ foo: "bar" }) // false //判斷並遍歷每個對象 if($("#t_specialsearch select").length <= 0){ alert("對象為空"); }else{ //console.log($("#t_specialsearch select")[0]); $("#t_specialsearch select").each(function(){ //alert($(this)); console.log($(this)); }); for(var i in $("#t_specialsearch select")){ console.log($("#t_specialsearch select")[i]); } }