주제 웹브라우저 html 문서 기본틀 정보를 저장하는 내장객체
document.멤버변수, document.메서드() 호출해서 사용
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test4.html</title>
<script type="text/javascript">
function fun1() {
alert(document.title);
}
</script>
</head>
<body>
<input type="button" value="문서제목출력" onclick="fun1()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test4.html</title>
<script type="text/javascript">
function fun2() {
// 문서 제목 변경 document.title변수에 값을 변경 => "문서제목"
document.title = "문서제목";
}
</script>
</head>
<body>
<input type="button" value="문서제목변경" onclick="fun2()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test4.html</title>
<script type="text/javascript">
function fun3() {
document.bgColor = "yellow";
}
</script>
</head>
<body>
<input type="button" value="배경색변경" onclick="fun3()">
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test4.html</title>
<script type="text/javascript">
function fun4(a) {
document.bgColor = a;
}
</script>
</head>
<body>
<!-- <input type="button" value="문서제목출력" onclick="fun1()"> -->
<!-- <input type="button" value="문서제목변경" onclick="fun2()"> -->
<!-- <input type="button" value="배경색변경" onclick="fun4()"> -->
<input type="radio" name="ra" onclick="fun4('green')"> green 색
<input type="radio" name="ra" onclick="fun4('skyblue')"> skyblue 색
<input type="radio" name="ra" onclick="fun4('pink')"> pink 색
</body>
</html>
radio 버튼 체크하면 배경색 바뀜
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test4.html</title>
<script type="text/javascript">
document.writeln("문서 내 출력")
</script>
</head>
<body>
</body>
</html>
화면 정보를 저장하는 내장객체
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test4.html</title>
<script type="text/javascript">
function fun5() {
alert(screen.availWidth); // 1920
alert(screen.availHeight); // 1040
alert(screen.orientation); // 방향
alert(screen.width); // 1920
alert(screen.height); // 1080 작업표시줄 포함한 크기
}
</script>
</head>
<body>
<input type="button" value="화면정보출력" onclick="fun5()">
</body>
</html>
자바스크립트 이용하여 웹문서에 접근, 제어할 수 있는 객체를 사용해 웹문서를 체계적으로 정리하는 방법
웹브라우저 이미지정보 저장하는 내장객체
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test5.html</title>
<script type="text/javascript">
function fun1() {
alert(document.img1.src);
alert(document.img1.width);
alert(document.img1.height);
alert(document.img1.border);
}
</script>
</head>
<body>
<img src="../html1/1.jpg" name="img1" width="200" height="300" border="1">
<input type="button" value="이미지정보확인" onclick="fun1()">
</body>
</html>
src
width
height
border
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test5.html</title>
<script type="text/javascript">
function fun2() {
document.img1.src="../html1/2.jpg";
document.img1.width = 250;
document.img1.height = 350;
document.img1.border = 2;
}
</script>
</head>
<body>
<img src="../html1/1.jpg" name="img1" width="200" height="300" border="1">
<input type="button" value="이미지변경" onclick="fun2()">
</body>
</html>
버튼 클릭 후 이미지 변경됨
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test5.html</title>
<script type="text/javascript">
function fun3() {
// img2 이미지를 src값 ../html1/3.jpg변경
// 테두리선 5 변경
document.img2.src="../html1/3.jpg";
documnet.img2.border=5;
}
</script>
</head>
<body>
<img src="../html1/2.jpg" name="img2" onclick="fun3()">
</body>
</html>
이미지 클릭 후 이미지 변경됨
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test5.html</title>
<script type="text/javascript">
function fun4() {
// img3 이미지를 src값 ../html1/4.jpg변경
document.img3.src="../html1/4.jpg";
}
function fun5() {
document.img3.src="../html1/3.jpg";
}
</script>
</head>
<body>
<img src="../html1/3.jpg" name="img3" onmouseover="fun4()" onmouseout="fun5()>
</body>
</html>
마우스 오버 후 이미지 변경됨
마우스 떼면 다시 원래 이미지로 변경됨
id선택자로 접근하는 getElementById() 메서드
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test5.html</title>
<script type="text/javascript">
function fun1() {
alert(document.getElementById("idimg1").src);
}
</script>
</head>
<body>
<img src="../html1/1.jpg" name="img1" width="200" height="300" border="1" id="idimg1">
<input type="button" value="이미지정보확인" onclick="fun1()">
</body>
</html>
alert(document.getElementById("idimg1").src);
==
alert(document.querySelector("#idimg2").src);
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test5.html</title>
<script type="text/javascript">
function fun3() {
document.getElementById("idimg2").src="../html1/3.jpg";
document.getElementById("idimg2").border=5;
}
</script>
</head>
<body>
<img src="../html1/2.jpg" name="img2" onclick="fun3()" id="idimg2">
</body>
</html>
주제 폼정보 저장하는 내장객체
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test6.html</title>
<script type="text/javascript">
function fun1() {
alert(document.fr.action);
alert(document.fr.method);
}
</script>
</head>
<body>
<form action="test1.html" method="get" name="fr" id="idfr">
텍스트 상자 : <input type="text" name="tx">
<input type="submit" value="전송">
<input type="button" value="폼정보" onclick="fun1()">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js2/test6.html</title>
<script type="text/javascript">
function fun2() {
document.fr.submit();
}
function fun3() {
document.fr.reset();
}
</script>
</head>
<body>
<form action="test1.html" method="get" name="fr" id="idfr">
텍스트 상자 : <input type="text" name="tx">
<input type="button" value="전송버튼" onclick="fun2()">
<input type="button" value="초기화버튼" onclick="fun3()">
</form>
</body>
</html>