반응형
함수
함수를 정의할 수 있다.
head 에 정의하고, body 에 호출한다.
function 함수명(파라메터 목록){
실행문;
return 값;
}
함수호출
함수명();
함수호출 예시
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
//함수 정의. 함수명: add. 파라메터 : a, b 2개
// 값 2개를 받아서 2개 더한 결과를 return
function add(a, b) {
return a + b;
}
</script>
</head>
<body>
<script type="text/javascript">
let res = add(1, 2);
document.write("res:" + res + "<br/>");
let res2 = add("aaa" + "bbb");
document.write("res2:" + res2 + "<br/>");
</script>
</body>
</html>
함수
버튼(button)과 함수(function)
<head>
<script type = "text/javascript">
function b(){
alert(f.btn1.value); //form f의 btn1의 value 를 출력(껍데기)
}
function c(){
alert("function c");
}
function d(){
alert("function d");
}
</script>
</head>
<body>
<form name = "f">
<input type = "button" name="btn1" value = "btn1" onclick="b()">
<input type = "button" value = "btn2" onclick="c()">
<input type = "button" value = "btn3" onclick="d()"><br/>
</form>
</body>
텍스트 값 지정 값으로 바꿔주기
<head>
<script type = "text/javascript">
function e(){ //텍스트 박스 값을 지정한 값으로 바꿔줌
f.val.value = "hello";
}
</script>
</head>
<body>
<form name = "f">
<input type = "text" name = "val">
<input type = "button" value = "확인" onclick="e()">
</form>
</body>
텍스트 값을 읽어오는 함수
<head>
<script type = "text/javascript">
function qq(){ //텍스트 박스 값을 읽어오는 함수
alert(f.val.value);
}
</script>
</head>
<body>
<form name = "f">
<input type = "text" name = "val">
<input type = "button" value = "확인2" onclick="qq()">
</form>
</body>
반응형