# http://msdn.microsoft.com/ko-kr/library/d1et7k7c(v=vs.94).aspx
# 노드관련 메서드
document.createElement("태그명")
document.createTextNode("텍스트")
객체명.insertBefore(newChild,refChild)
객체명.replaceChild(newChild,oldChild)
객체명.removeChild(oldChild)
객체명.appendChild(newChild)
객체명.parentNode
객체명.childNodes
객체명.previousSibling
객체명.nextSibling
버튼을 누르면 4.dd 가 출력될 수 있도록 소스코드를 작성하시오.
<html>
<head>
<script>
window.onload=function(){
b.onclick=function(){
//here
}
}
</script>
</head>
<body>
<ol>
<li>aa</li>
<li>bb</li>
<li>cc</li>
</ol>
<input type="button" id="b">
</body>
</html>
[an]
var liTag=document.createElement("li");
liTag.innerHTML="dd";
olTag.appendChild(liTag);
버튼을 누르면 칸이 추가되게 소스코드를 작성하시오.
<html>
<head>
<style type="text/css">
td { border : 1px solid green; }
</style>
<script>
window.onload=function(){
b.onclick=function(){
//here
}
}
</script>
</head>
<body>
<table>
<tr><td>1</td><td>2</td></tr>
</table>
<input type="button" id="b" value="칸추가">
</body>
</html>
[an]
<html>
<head>
<style type="text/css">
td { border : 1px solid green; }
</style>
<script>
window.onload=function(){
var num=3;
b.onclick=function(){
var tdTag=document.createElement("td");
tdTag.innerHTML=num++;
trTag.appendChild(tdTag);
}
}
</script>
</head>
<body>
<table>
<tr id="trTag"><td>1</td><td>2</td></tr>
</table>
<input type="button" id="b" value="칸추가">
</body>
</html>
알파벳 순서대로 출력될 수 있도록 소스코드를 수정하시오.
<html>
<head>
<script>
window.onload=function(){
b.onclick=function(){
var liTag=document.createElement("li");
liTag.innerHTML="cc";
olTag.appendChild(liTag);
}
}
</script>
</head>
<body>
<ol id="olTag">
<li>aa</li>
<li>bb</li>
<li>dd</li>
</ol>
<input type="button" id="b">
</body>
</html>
[an]
var liTag=document.createElement("li");
liTag.innerHTML="cc";
olTag.insertBefore(liTag,ref);
'칸삭제'버튼을 눌렀을 때 뒷칸이 제거되게 하시오.
<html>
<head>
<style type="text/css">
td { border : 1px solid green; }
</style>
<script>
window.onload=function(){
var num=3;
b1.onclick=function(){
var tdTag=document.createElement("td");
tdTag.innerHTML=num++;
trTag.appendChild(tdTag);
}
b2.onclick=function(){
//here
}
}
</script>
</head>
<body>
<table>
<tr id="trTag"><td>1</td><td>2</td></tr>
</table>
<input type="button" id="b1" value="칸추가">
<input type="button" id="b2" value="칸삭제">
</body>
</html>
[an]
var index=trTag.childNodes.length-1;
trTag.removeChild(trTag.childNodes[index]);
문제
1번버튼클릭시 --> bbb<태그>aaa</태그>
2번버튼클릭시 --> <태그>bbbaaa</태그>
3번버튼클릭시 --> <태그>aaabbb</태그>
4번버튼클릭시 --> <태그>aaa</태그>bbb
# 객체명.insertAdjacentHTML(sWhere, "<i>bbb</i>");
beforeBegin 시작태그전에
afterBegin 시작태그후에
beforeEnd 종료태그전에
afterEnd 종료태그후에
<html>
<head>
<script>
function a(){
o.insertAdjacentHTML("beforeBegin","<i>bbb</i>");
alert(o2.innerHTML);
}
</script>
</head>
<body>
<input type="button" value="beforeBegin" onclick="a(this)">
<input type="button" value="afterBegin" onclick="a(this)">
<input type="button" value="beforeEnd" onclick="a(this)">
<input type="button" value="afterEnd" onclick="a(this)">
<div id="o2"><span id="o">aaa</span> </div>
</body>
</html>
[ex] 주석처리부분을 for문으로 바꾸고
텍스트가 포커스를 얻으면 배경색상이 오렌지로 바뀌고
텍스트가 포커스를 잃어버리면 배경색상이 원래대로
바뀔수 있도록 소스코드를 작성하시오.
<html>
<body>
<form>
<!--
<input type="text">
<input type="text">
<input type="text">
<input type="text">
<input type="text">
-->
</form>
</body>
</html>
[an]
<html>
<head>
<style type="text/css">
</style>
<script>
window.onload=function(){
for(var a=0; a<5; a++){
document.forms[0].elements[a].onfocus=f1;
document.forms[0].elements[a].onblur=f2;
}
}
function f1(){
this.style.background="orange";
}
function f2(){
this.style.background="";
}
</script>
</head>
<body>
<form>
<script>
for(var a=0; a<5; a++)
document.write("<input type=text>");
</script>
</form>
</body>
</html>
문제
<html>
<head>
<style type="text/css">
td {border:2px solid green; width:50px;
</style>
<script>
window.onload=function(){
//줄칸을 입력받아 테이블을 만드시오
}
</script>
</head>
<body>
<input type="text" id="t1">줄
<input type="text" id="t2">칸
<input type="button" value="테이블" id="buttonTag">
<div id="result"></div>
</body>
</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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | # http://msdn.microsoft.com/ko-kr/library/d1et7k7c(v=vs.94).aspx # 노드관련 메서드 document.createElement("태그명") document.createTextNode("텍스트") 객체명.insertBefore(newChild,refChild) 객체명.replaceChild(newChild,oldChild) 객체명.removeChild(oldChild) 객체명.appendChild(newChild) 객체명.parentNode 객체명.childNodes 객체명.previousSibling 객체명.nextSibling [ex] 버튼을 누르면 4.dd 가 출력될 수 있도록 소스코드를 작성하시오. <html> <head> <script> window.onload=function(){ b.onclick=function(){ //here } } </script> </head> <body> <ol> <li>aa</li> <li>bb</li> <li>cc</li> </ol> <input type="button" id="b"> </body> </html> [an] var liTag=document.createElement("li"); liTag.innerHTML="dd"; olTag.appendChild(liTag); [ex] 버튼을 누르면 칸이 추가되게 소스코드를 작성하시오. <html> <head> <style type="text/css"> td { border : 1px solid green; } </style> <script> window.onload=function(){ b.onclick=function(){ //here } } </script> </head> <body> <table> <tr><td>1</td><td>2</td></tr> </table> <input type="button" id="b" value="칸추가"> </body> </html> [an] <html> <head> <style type="text/css"> td { border : 1px solid green; } </style> <script> window.onload=function(){ var num=3; b.onclick=function(){ var tdTag=document.createElement("td"); tdTag.innerHTML=num++; trTag.appendChild(tdTag); } } </script> </head> <body> <table> <tr id="trTag"><td>1</td><td>2</td></tr> </table> <input type="button" id="b" value="칸추가"> </body> </html> [ex] 알파벳 순서대로 출력될 수 있도록 소스코드를 수정하시오. <html> <head> <script> window.onload=function(){ b.onclick=function(){ var liTag=document.createElement("li"); liTag.innerHTML="cc"; olTag.appendChild(liTag); } } </script> </head> <body> <ol id="olTag"> <li>aa</li> <li>bb</li> <li>dd</li> </ol> <input type="button" id="b"> </body> </html> [an] var liTag=document.createElement("li"); liTag.innerHTML="cc"; olTag.insertBefore(liTag,ref); [ex] '칸삭제'버튼을 눌렀을 때 뒷칸이 제거되게 하시오. <html> <head> <style type="text/css"> td { border : 1px solid green; } </style> <script> window.onload=function(){ var num=3; b1.onclick=function(){ var tdTag=document.createElement("td"); tdTag.innerHTML=num++; trTag.appendChild(tdTag); } b2.onclick=function(){ //here } } </script> </head> <body> <table> <tr id="trTag"><td>1</td><td>2</td></tr> </table> <input type="button" id="b1" value="칸추가"> <input type="button" id="b2" value="칸삭제"> </body> </html> [an] var index=trTag.childNodes.length-1; trTag.removeChild(trTag.childNodes[index]); 문제 1번버튼클릭시 --> bbb<태그>aaa</태그> 2번버튼클릭시 --> <태그>bbbaaa</태그> 3번버튼클릭시 --> <태그>aaabbb</태그> 4번버튼클릭시 --> <태그>aaa</태그>bbb # 객체명.insertAdjacentHTML(sWhere, "<i>bbb</i>"); beforeBegin 시작태그전에 afterBegin 시작태그후에 beforeEnd 종료태그전에 afterEnd 종료태그후에 <html> <head> <script> function a(){ o.insertAdjacentHTML("beforeBegin","<i>bbb</i>"); alert(o2.innerHTML); } </script> </head> <body> <input type="button" value="beforeBegin" onclick="a(this)"> <input type="button" value="afterBegin" onclick="a(this)"> <input type="button" value="beforeEnd" onclick="a(this)"> <input type="button" value="afterEnd" onclick="a(this)"> <div id="o2"><span id="o">aaa</span> </div> </body> </html>
| cs |
'매일코딩 > 자바스크립트예제' 카테고리의 다른 글
왕 기초 자바스크립트 - 배열 (4) | 2016.12.28 |
---|---|
10.자바스크립트 기초 문법 (노드메서드 2) (2) | 2016.12.27 |
9.자바스크립트 기초 문법 (노드메서드) (4) | 2016.12.27 |
★자바스크립트 연습 문제2 (2) | 2016.12.26 |
8.자바스크립트 기초 문법 (confirm&prompt&parseInt&이벤트처리&String&DATE) (4) | 2016.12.26 |
댓글