Jquery Js 判断对象存在的区别

$("#ID")获取的永远是对象  即使网页上没有此元素  应该根据获取到的元素的长短来进行判断

代码:

       $(document).ready(
            function()
            {
                if($("#ex").length>0)//根据获取到的元素的长短来进行判断
               {
                  alert("it exists");
               }
               else
               {
                    alert("it not exists");
               }
            }
      
       );

 

//Jquery转换为DOM判断

        $(document).ready(
            function()
            {
               if($("#ex")[0]) //DOM对象
               {
                  alert("it exists");
               }
               else
               {
                    alert("it not exists");
               }
            }
      
       );

 

//js 判断对象是否存在
        window.onload=function()
       {
            if(document.getElementById("ex"))
               {
                  alert("it exists");
               }
               else
               {
                    alert("it not exists");
               }
       }

posted @ 2012-04-21 11:17  sidihu  阅读(174)  评论(0编辑  收藏  举报