본문 바로가기
HTML│CSS│Java Script

html

by 자유코딩 2017. 12. 6.
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

'HTML│CSS│Java Script' 카테고리의 다른 글

html  (0) 2017.12.06
html  (0) 2017.12.06
html 예제  (0) 2017.12.06
html 예제  (0) 2017.12.06
p 태그 id에 자바스크립트 결과 담기  (0) 2017.12.06

댓글