자바스크립트와 jQuery 각각의 사용 방법을 정리해본다.

test라는 id를 갖고 있는 객체가 있는지 찾아보자 !

<script type="text/javascript">
 
window.onload = function(){
 
    // javascript
    if(document.getElementById("test")){
        console.log("id=test 객체가 존재합니다.");
    }else{
        console.log("id=test 객체가 존재하지 않습니다.");
    }
 
 
    // jQuery
    if($("#test").length > 0){
        console.log("id=test 객체가 존재합니다.");
    }else{
        console.log("id=test 객체가 존재하지 않습니다.");
    }
}
 
</script>

 

jQuery는 객체가 존재하지 않으면 length를 0으로 리턴한다.

Posted by useways
,