<html>
<head>
<style type="text/css">
</style>
<script>
window.onload = function() {
b.onclick = function() {
if (txt1.value.trim()) {
if (isNaN(txt1.value)) {
alert("숫자가 아니에용");
txt1.value = "";
txt2.value = "";
txt3.value = "";
txt1.focus();
} else {
if (txt1.value > 5 || txt1.value < 1) {
alert("1~5까지의 숫자를 입력하세요!!");
txt1.value = "";
txt2.value = "";
txt3.value = "";
txt1.focus();
} else {
txt2.value = Math.ceil(Math.random() * 5);
if (txt1.value == txt2.value)
txt3.value = "행운 팡팡팡~";
else
txt3.value = "꽝~!!!!";
}
}
} else if (!txt1.value.trim()) {
alert("공백입력하면 안되지용");
txt1.focus();
}
}
}
</script>
</head>
<body>
<h1>1부터 5까지의 값을 입력하세요~ ^^</h1>
내가 입력한 숫자:
<input type="text" id="txt1">
<input type="button" id="b" value="결과보기">
<p>
컴퓨터 숫자:<input type="text" id="txt2"> 결과: <input type="text"
id="txt3">
</body>
</html>
'개발하자 > 자바스크립트' 카테고리의 다른 글
과제3(마우스클릭 좌표값을 이용해 div출력하기) (0) | 2015.02.24 |
---|---|
과제2(선택한 링크의 내용 텍스트박스에 출력하기) (0) | 2015.02.24 |
Math객체 (0) | 2015.02.24 |
객체를 생성하는 방식 두가지 (0) | 2015.02.24 |
event객체 visibility-visible,hidden (0) | 2015.02.24 |