테스트 사이트 - 개발 중인 베타 버전입니다

자바스크립트 이벤트객체

· 19년 전 · 3763 · 2
1.



예) window.document.form1.id.value="lee"

코드설명: 현제 브라우저의 창에 존재하는 문서 내용중 "form1" 안에 "id" 객체의 값은 "lee"이다.



주요객체

window : 윈도우마다 하나씩 존재하는 객체로서 윈도우 내에 존재하는 모든객체의 상위 객체이다.

frame:프레임마다 존재하는 객체<frame>태그에 의해 정의 된다.

document : html 페이지마다 존재하는 객체, html문서에 대한 정보를 가진다.

location : 현재 로드된 html페이지의 url에 대한 정보

history : 현재 사용자가 방문하였던 url 들이 리스트를 우지하는 객체이다.

form : 폼에대한 정보를 표헌하는 객체로서 <form>태그당 하나의 객체



계층구조에 따라서 자바 스크립트에 접근





<html>

<head>

<title>4-1 계층구조로 객체사용 </title>

</head>

<body>
<form name="f">

<input type ="text" name ="a" value ="나는 자바 스크립트!">

<input type ="text" name ="b" ><p>

<input type ="button" value="복사" onClick ="document.f.b.value = document.f.a.value"><br>

-->******** documet.f.b.value 와 this.form.b.value와 같다. *******

<input type ="button" value="지우기" onClick="this.form.b.value=''" ><p>

</form>

</body>
</html>



****이벤트 ****



이벤트 처리이벤트



abort Onabort : 이미지를 읽는 중에 중지했을때 발생

blur OnBlur :입력양식 필드에서 포커스를 일어 버렸을대 발생(커서가 깜빡일때)

changge OnChange: 입력양식 필드에서 값이 바뀌었을때 발생

click OnClick : 입력양식필드에서 마ㅅ우스를 눌렀을때 발생

dblclick OndblClick : 마우스를 두 번 눌렀을때 발생

dragdrop OnDragDrop: 마우스를 클릭한 상태에서 드래그 했을때 발생

error OnError : 문서나 이미지를 읽는 중에 중지 했을때 발생

focus OnFocus : 입력양식 필드에 포커스가 위치할때 발생

keydown OnKeyDown : 키를 눌렀을때 발생

keypress OnKeyPress : 키를 눌렀다가 놓았을대 발생

keyup OnKeyUp : 키를 놓았을때

load OnLoad :브라우저의 문서를 읽어올때 발생

mousedown OnMouseDown : 마우스 버튼을 눌렀을때 발생

mousemove OnMouseMove : 마우스 위치를 옮겼을때 발생

mouseout OnMouseOut : 마우스가 링크나 영역안에 있다가 벗어났을때 발생

mouseover OnMouseOver : 링크위로 마우스가 위치할때 발생

mouseup OnMouseUp : 마우스를 놓았을때 발생

move OnMove : 프레임이나 우인도를 이동했을 경우에 발생

reset OnReset : 입력양식을 다시 초기화 시켰을때 발생

resize OnResize : 프레임이나 윈도우 크ㄹ기를 변경했을 경우 발생

select Onselect : 입력양식의 한필드를 선택했을때 발생

unload OnUnload : 브라우저의 문서를 닫을때 발생





이벤트 처리의 특징

* 이벤트핸들러의 설정은 window상에서 click 이벤트가 발생하면 자바 스크립트 합수 xxx() 가 실행되게 할때

window.onClick()=xxx(); 라고 입력





<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>4-2</title>
</head>

<body onLoad = window.open() onBlur ="document.bgColor='blue'" onFocus = "document.bgColor= 'red'">
<h3>포커스를 받으면 빨간색, 잃으면 파란색 배경응로 변합니다. </h3>
</body>
</html>


예) 텍스트 창에 어떤 문자가 입력될때까지 다른 일을 할수 없게 하는 예





<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>무제 문서</title>
<script>
function aaa(text){

if(text.value=""){
arlert("이름을 입력하세요");
text.focus();
}
}
</script>

</head>

<body>
<form name="f">
이름<input type="text" name ="t" onBlur="aaa(this)"><br>aaa(this)-->this매개변수는<input type....>에서 의 값을 a()에 넘겨주는 것을 의미
학번<input type= "text" name="id" >
</form>
</body>
</html>


**onChange 핸들러

예) 4글자를 초과하지 못하게 하는 text창 만들기, select 객체의 옵션이 변경되면 변경된 내용을 출력



<html><head>

<script>



function check(text){

if(text.value.length){

alert("입력가능한 최대글자수는 4자입니다.")

text.value=text.value.substring(0,4)

text.focus()

}

}

function checksel(sel){

alert("당신이 선택한 항목은"+sel.options.value)

}

</script></head>

<body>

이름을 입력하는 난에 4글자를 초과하여 입력하면 경고 메세지 출력, 과일 선택이 변경되면 선택된 과일을 경고 메시지 박스와 함께 출력하자

<form name="f">

이름<input type="text" onChange="check(this)"></br>

과일<select name="select" onChange="checksel(this)">

<option value="사과">사과

<option value ="감">감

<option value="배">배

</select>

</form>

</body>

</html>







*onLoad핸들러 : 윈도우 상에서 문서의 로딩 완료 시전ㅁ과 이ㅈ미지의 로딩 완료 시점에 생성

1. 윈도우(프레임)상에서 로딩완료 <body onLoad="">,<frameset onLoad="">

2. 이미지 로딩 완료:<img onLoad ="">



<html>

<body onLoad="alert('어서오세요. 반갑습니다.')"onunLoad="alert('안녕히 가세요.')">

<body>

</html>





*onClick과 onDblClick



예제)계산기 만들기





<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
<title>무제 문서</title>
<script>
function compute(f){
var res= eval(f.exp.value); --->eval()함수는 변수를 자바스크립트 의 함수처럼쓰는 명령어 문자열로 입력된 수식을 계산해

주는 내장함수입니다.
f.val.value = res;
}

</script>
</head>
<body>
식난에 계산하고자 하는 수입력후 계산 버튼을 누르시요.<p>

<form name="cal">

식<input type="text" name= "exp" value=""><p>

값<input type="text" name =" val" value="" size="10">

<input type= button value="계산" onClick("computer(this.form)")>(computer(this.form)->객체) 은 computer(document.cal) 과 같다

</form>
</body></html>



[펌] http://blog.naver.com/kdmsal82?Redirect=Log&logNo=150007107406<div class='small'>[이 게시물은 관리자님에 의해 2011-10-31 17:16:08 PHP & HTML에서 이동 됨]</div>

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 2개

19년 전
좋은 정보 감사합니다...
document.form.id... 라는 것은 이젠 옛말이 아닐까 합니다.

앞으로 나올 그누5나, 다음이라도 해당 코드는

document.forms['form'].elements['id'] 가 되야 될 것 입니다.

게시글 목록

번호 제목
10705
10698
10697
10693
10687
10683
10681
10680
10677
10671
10670
10666
10664
10658
10656
10653
10652
10651
10650
10649
10648
10646
10645
10642
28809
10641
10638
10637
10636
10635
28808
10634
10632
28807
28806
10631
10630
10629
28805
10627
10626
10625
28804
28803
10624
10623
28802
10622
10620
10619
10618
10616
10615
10613
10611
28799
10610
10608
28798
10604
28794
10602
10600
28793
10596
10595
10593
28792
28791
28790
10592
28789
28787
10591
10590
10589
28785
10588
10587
28783
10586
28782
10585
10574
10571
10570
28778
10568
10567
28777
10564
10562
10561
28776
10559
10558
28775
28774
10557
10556