Script 기본 사용법 (2)

2019. 3. 27. 16:12JavaScript

#1 이중 함수 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

<%@ page language="java" contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>Ex_03</title>

<script>

/* window.onload를 사용하여 문서의 모든 내용을 먼저 인지한 후에 함수를 실행하므로 위치와 관계없이

실행이 가능하다. => 함수 중첩 */

window.onload = function(){

//////////

var btn2 = document.getElementById('btn2');

btn2.onclick = function(){

alert('버튼2이 클릭됨 ');

};

///////이중함수///////

var btn3 = document.getElementById('btn3');

btn3.ondblclick = function(){ // 더블클릭

/* 실행 X

fun1();

var fun3 = function(){

alert();

}

*/

funBtn3();

function funBtn3(){ // 함수 선언문 => 브라우저는 함수 선언문부터 우선적으로 로딩

alert('버튼');

};

};

};

 

// 함수정의 후 바로 호출 name,age에 각각 매개값1,2가 입력됨

(function(name,age){

alert('익명함수2 호출됨'+ name+' ' +age);

}('매개값1', '매개값2'));

 

</script>

</head>

<body>

<button type="button" id="btn1">버튼 1</button>

<button type= "button" id ="btn2">버튼 2</button>

<button type= "button" id ="btn3">버튼 3</button>

 

<script>

// 버튼이 먼저 인지되어야 아래의 문장이 실행이 가능하다.

var btn1 = document.getElementById('btn1'); // 버튼 객체의 해당 id를 변수에 저장

btn1.onclick = function(){

alert('버튼1이 클릭됨 ');

};

</script>

</body>

</html>

Colored by Color Scripter

cs