본문 바로가기
  • think normal
새로워지기/서른의 생활코딩

샘플소스1) javascript 기본 문서구조 ( if문 과 if else문 )

by 청춘만화 2011. 4. 5.



if문

예제 5)



<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">

<head>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />

<title> IF  </title>

<meta name="Keywords" content="" />

<meta name="Description" content="" />

</head>

<body>


<p>test"1"</p>

<p>test2</p>


<script type="text/javascript">

//<!CDATA[

var reply=confirm("열심히 공부를 하겠습니다");

if(reply==true){

alert(" 열심히하세요");

}

//]]]]>

</script>


</body>

</html>





 if else문 

예제 6)


기본 조건


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> IF else 문 </title>


<meta name="Keywords" content="" />


<meta name="Description" content="" />


</head>


<body>




<script type="text/javascript">


//<!CDATA[


var age=20;




if(age>=20){


alert("입장가능");


}else{


alert("입장불가");


}


//]]]]>


</script>




</body>


</html>



조건 추가


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> IF else 문 </title>


<meta name="Keywords" content="" />


<meta name="Description" content="" />


</head>




<body>


<script type="text/javascript">


//<!CDATA[


var age=17;


var sex="men";


/*
조건:17~19,조건은 남자만 입장가능


var reply1=confirm("17~19세입니까?");


var reply2=confirm("남자입니까?");




if(reply1==true){


if(reply2==true);


alert("입장하세요.");


}else{


alert("입장불가 입니다.");


}


*/


if((age>=17&&age<=19)&&sex=="men"){


alert("합격입니다.");


}else{


alert("불합격입니다.");


}


//]]]]>


</script>


</body>




</html>



조건 추가 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> 조건문3 </title>


<meta name="Keywords" content="" />


<meta name="Description" content="" />


</head>


<body>




<script type="text/javascript">


//<!CDATA[


var name=prompt("이름을 입력하세요","");


document.write(name);


/*


if(name==null){


alert(name+"님.. 방가방가..");


}else{


alert("이름을 입력하시라구요");


}


*/


if(name==""){


alert("이름을 입력하시라구요");


}else{


alert(name+"님.. 방가방가..");


}


//]]]]>


</script>




</body>


</html>



조건 활용


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"


"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ko" lang="ko">




<head>


<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />


<title> 조건문4 </title>


<meta name="Keywords" content="" />


<meta name="Description" content="" />


</head>


<body>




<script type="text/javascript">


//<!CDATA[


var number=prompt("좋아하는 숫자를 하나 넣어주세요","");


document.write(number);




if(number%2==0){


alert("짝수입니다.");


}else{


alert("홀수입니다.");


}


//]]]]>


</script>




</body>


</html>

댓글