반응형
자바스크립트를 이용해 페이지내에서 특정 DOM 객체가 존재하는지 확인해야 할 필요가 가끔 있다.
이럴때는 DOM 객체에 ID를 부여하고 해당 ID를 체크하면 된다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>DOM ID Check</title>
<script type="text/javascript">
window.onload = function(){
// 자바스크립트
if(document.getElementById("test")){
console.log("해당 객체 존재함");
}else{
console.log("해당 객체 존재안함");
}
// jQuery
if($("#test").length > 0){
console.log("jquery : 해당 객체 존재함");
}else{
console.log("jquery : 해당 객체 존재안함");
}
}
</script>
</head>
<body>
<input type="text" name="test" id="test" value="">
</body>
</html>
jQuery에서는 ID가 존재하지 않을경우 length 로 0을 리턴한다.
'Language > JQuery' 카테고리의 다른 글
[jQuery] ajaxForm을 이용한 ajax 파일 업로드 (1) | 2017.06.13 |
---|---|
[jQuery] ajax 전역 이벤트(Ajax Global Event) (1) | 2017.03.06 |
[jQuery] IE Ajax cache 관련 오류. (1) | 2016.10.13 |
[JQuery] input 객체 - readonly, disabled 처리 (1) | 2016.07.06 |
[JQuery] input 배열 값 가져오기 (3) | 2016.05.11 |