개발자의 길/Javascript
자바스크립트 이벤트(Event) 02
모쿠
2017. 4. 7. 13:40
<자바스크립트 Visibility/Display 속성 변경>
* button 태그의 type 속성:
1. button: 클릭할 수 있는 버튼
2. submit: form의 데이터들을 서버로 전송(type 속성의 기본값!)
3. reset: form의 입력된 데이터들을 리셋(지움)
<28_event2.html>
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>JavaScript</title> <style type="text/css"> h1{ background-color: pink; color: white; font-size: 2.5em; /* 250% */ text-align: center; } .main-panel { font-size: 2em; font-weight: bold; text-align: center; background-color: lightgreen; color: white; } .sub-panel { font-size: 1em; font-weight: normal; color: black; text-align: left; border-top: 1px solid white; background-color: lavender; display: none; } ul { list-style-type: none; } </style> </head> <body> <h1 style="visibility: hidden;" id="title">Visibility/Display 속성 변경</h1> <button type="button" onclick="hide()">Hide</button> <button type="button" onclick="show()">Show</button> <button type="button" onclick="none()">Display None</button> <button type="button" onclick="block()">Display Block</button> <hr/> <div class="main-panel" onclick="toggleSub(this)"> Main Panel - Click Me! <div class="sub-panel"> <ul> <li><a href="http://www.google.com">구글</a></li> <li><a href="http://www.daum.net">다음 카카오</a></li> <li><a href="http://www.naver.com">네이버</a></li> </ul> </div> </div> <script type="text/javascript"> var title = document.getElementById('title'); function hide() { title.style.visibility = 'hidden'; } function show() { title.style.visibility = 'visible'; } function none() { title.style.display = 'none'; } function block() { title.style.display = 'block'; } function toggleSub(parent) { // .sub-panel 요소(element)를 찾음 // var sub = document.getElementsByClassName('sub-panel')[0]; // var sub = document.querySelector('.sub-panel'); // var sub = document.getElementById('sub'); var sub= parent.firstElementChild; if(sub.style.display === 'none' || sub.style.display === ''){ // display: none; 이면, 보이게 sub.style.display = 'block'; } else { // displaty: block; 이면, 안보이게 sub.style.display = 'none'; } } </script> </body> </html> | cs |
<출력화면>
- 초기화면
- Show 클릭
- Display None 클릭
- Display Block 클릭
- 클릭 전
- 클릭 후