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 |
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DOM</title>
<style type="text/css">
/* 요소 선택자 */
div{background-color:skyblue;height:100px}
</style>
<script type="text/javascript">
function change_go(){
document.getElementById("mydiv").style.background="yellow";
}
/*body 부분의 구조를 읽고 난 뒤에 실행하세요*/
/*페이지 로드 이벤트*/
window.onload=function(){
var btn1=document.getElementById("btn1");
btn1.onclick=function(){
document.getElementById("mydiv").style.color="red";
document.getElementById("mydiv").style.width="100px";
}
}
</script>
</head>
<body>
<!--DOM(Document Object Model)-->
<!--웹 브라우저가 html 페이지를 인식하는 방식-->
<!--노드 : html의 각 요소(element) = 태그-->
<!--DOM은 트리 구조이다. 형제 노드, 부모 노드 , 자식 노드-->
<div id = "mydiv">
<span>hi</span>hello
</div>
<button onclick="change_go()">스타일 변경</button>
<input type="button" id="btn1" value="스타일 변경"/>
</body>
</html> |
cs |
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 |
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>동일 요소 검색 : getElementsByTagName</title>
<script type="text/javascript">
onload = function(){
document.getElementById("btn").onclick = function(){
var count = document.getElementsByTagName("li");
//alert(count.length);//10
var s ="";
for (var i = 0; i < count.length; i++) {
//s=s+count[i].firstChild.data;//텍스트 추출
s=s+count[i].getAttribute("value");//속성 추출
count[i].getAttribute("type","square");//속성 추출
if(i%2==0){
count[i].style.background="green";
}
}
document.getElementById("res").innerHTML = s;
}
}
</script>
</head>
<body>
<ul>
<li value="1">java1</li>
<li value="2">java2</li>
<li value="3">java3</li>
<li value="4">java4</li>
<li value="5">java5</li>
</ul>
<ul>
<li value="java1">java1</li>
<li value="java2">java2</li>
<li value="java3">java3</li>
<li value="java4">java4</li>
<li value="java5">java5</li>
</ul>
<button id="btn">눌러주세요</button>
<br/>
<h2>결과보기 : <span id="res"></span></h2>
</body>
</html> |
cs |
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 |
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>form 요소 찾기</title>
<script type="text/javascript">
onload = function(){
document.getElementById("btn1").onclick=function(){
var res1 = document.getElementById("target1").value;
var res2 = document.f.name.value;
document.getElementById("res1").innerHTML = res1;
document.getElementById("res2").innerHTML = res2;
};
document.getElementById("btn2").onclick=function(){
var res1 = document.forms[1].elements[0].value;
var res2 = document.forms[1].pw.value;
document.getElementById("res1").innerHTML = res1;
document.getElementById("res2").innerHTML = res2;
};
}
</script>
</head>
<body>
<form name="f">
<input type="text" id="target1" name="name"/>
<input type="button" id="btn1" value="실행"/>
</form>
<form>
<input type="text" id="target2" name="id"/>
<input type="text" id="target3" name="pw"/>
<input type="button" id="btn2" value="실행"/>
</form>
<br/>
<hr/>
<h2>결과보기 : <span id="res1"></span></h2>
<h2>결과보기 : <span id="res2"></span></h2>
</body>
</html> |
cs |
'HTML│CSS│Java Script' 카테고리의 다른 글
html / css (0) | 2017.12.06 |
---|---|
html (0) | 2017.12.06 |
html (0) | 2017.12.06 |
html 예제 (0) | 2017.12.06 |
html 예제 (0) | 2017.12.06 |
댓글