2012-06-12 1 views
3

jQuery를 사용하여 HTML에서 객체를 찾으려고합니다.jQuery에서 HTML 객체가 null인지 어떻게 확인합니까?

function getHats() { 
    $.get('http://www.roblox.com/User.aspx?ID=1', 
     function parse(data) { 
      var id1 = $(data).find('#ctl00_cphRoblox_rbxUserAssetsPane_UserAssetsDataList_ctl09_AssetThumbnailHyperLink'); 
      hatname1 = id1.attr('title'); 
      hatlink1 = "http://www.roblox.com" + id1.attr('href'); 
      hatpic1 = id1.find('img').attr('src'); 
      var lim1 = id1.parent('.AssetThumbnail').find('div'); 
      if(!lim1) { 
       hatlim1 = true; 
       alert("Null") 
      }else{ 
       hatlim1 = false; 
      }; 
     } 
    ); 
}; 

항상 경고가 나타납니다.

답변

6

사용 길이 -하지만 당신은 그것에 대해 명시 될 필요가 없습니다은 :

if (lim1.length) { 
    // lim1 has a length! And it's not 0! 
} else { 
    // arg, lim1 has a length of 0 
} 

동등하게, 부정 연산자 !는 잘 작동합니다.

if (!lim1.length) { 
    // arg! 
} 
3

이 경우 length을 사용할 수 있습니다.

if(lim1.length < 1){ 
    //alert 
} 
관련 문제