[JavaSctipt] ๐ŸŒŽ HTML DOM ๊ฐ์ฒด

์ „์ฃผ์€ยท2022๋…„ 11์›” 30์ผ
0
post-thumbnail

๐ŸŒŽ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ HTML DOM ๊ฐ์ฒด

๐Ÿง ๋“ค์–ด๊ฐ€๊ธฐ์ „์—..

์›น๊ฐœ๋ฐœ ํ• ๋•Œ ํ•„์ˆ˜์ธ HTML DOM์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค!! ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(Document Object Model)์€ HTML๋ฌธ์„œ ๋˜๋Š” XML๋ฌธ์„œ๋“ฑ์„ ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ผ์ข…์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ๋ฌธ์„œ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์š”์†Œ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณต ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“„ document ๊ฐ์ฒด

  • ์›น ํŽ˜์ด์ง€ ์ž์ฒด(body)๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฐ์ฒด
  • ์›น ํŽ˜์ด์ง€์— ์กด์žฌํ•˜๋Š” HTML ์š”์†Œ์— ์ ‘๊ทผํ•˜๊ณ ์ž ํ•  ๋•Œ ๋ฐ˜๋“œ์‹œ document ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์‹œ์ž‘

document ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ

  • getElementsByTagName(): ํ•ด๋‹น ํƒœ๊ทธ ์ด๋ฆ„์˜ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•ด ๋ฐฐ์—ด๋กœ ์ €์žฅ
  • getElementById(): ํ•ด๋‹น ์•„์ด๋””์˜ ์š”์†Œ๋ฅผ ์„ ํƒ โ—โ—์ฃผ์˜: s์—†์Œ!! id๋Š” ํ•˜๋‚˜๋‹ˆ๊นŒ!!โ—โ—
  • getElementsByClassname(): ํ•ด๋‹น ํด๋ž˜์Šค์— ์†ํ•œ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•ด ๋ฐฐ์—ด๋กœ ์ €์žฅ
  • getElementsByName(): ํ•ด๋‹น name ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•ด ๋ฐฐ์—ด๋กœ ์ €์žฅ
  • querySelectorAll(): ์„ ํƒ์ž๋กœ ์„ ํƒ๋˜๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•ด ๋ฐฐ์—ด๋กœ ์ €์žฅ
    (querySelector()):

๐Ÿ“‹ form ๊ฐ์ฒด

  • ์ผ๋ฐ˜์ ์ธ ํผ์— ์ ‘๊ทผํ•  ๋•Œ ์‚ฌ์šฉ, document.forms ์ปฌ๋ ‰์…˜์„ ์ด์šฉํ•ด์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ
  • document: body๋ถ€๋ถ„ -> document.name: body์•ˆ name์„ ์ฐพ์Œ

ํผ ์ ‘๊ทผ

  <form name = "myform" id = "regform" method="post" action="regist.jsp">
      ์•„์ด๋””:<input type = "text" name="userid" id="id"><br>
      ๋น„๋ฐ€๋ฒˆํ˜ธ: <input type = "password" name="userpw" id= "pw"><br>
  </form>
  • name์œผ๋กœ ์ฐพ๊ธฐ
  const frm = document.myform;
  const frm = document.forms['myform']
  • id๋กœ ์ฐพ๊ธฐ
  const frm = document.getElementById('regform')
  • nth๋กœ ์ฐพ๊ธฐ
  const frm = document.forms[0];    //์ฒซ๋ฒˆ์งธ ํผ์„ ์ฐพ๋Š”๋‹ค.

ํผ ์•ˆ์— ์•„์ด๋”” ์ž…๋ ฅ์ƒ์ž ์ ‘๊ทผ

  • name์œผ๋กœ ์ฐพ๊ธฐ
  const userid = frm.userid;//์œ„์— form์ ‘๊ทผํ•˜๋Š” ๋„ท ์ค‘ ํ•˜๋‚˜ ์ ์€ํ›„
  const userid = document.forms['myform'].elements[0];	//ํ•œ๋ฒˆ์— ์ ์–ด๋„ ๋ฌด๊ด€
  • id๋กœ ๋ฐ”๋กœ ์ฐพ์•„์ฃผ๊ธฐ
  const userid = document.getElementById('id');
  • nth๋กœ ์ฐพ๊ธฐ
  const userid = document.forms['myform'].elements['userid'];
  const userid = document.forms[0][0] 

๐ŸŽ ์ด๋ฒคํŠธ ๊ฐ์ฒด(Event Object)

  • ํŠน์ • ํƒ€์ž…์˜ ์ด๋ฒคํŠธ์™€ ๊ด€๋ จ์ด ์žˆ๋Š” ๊ฐ์ฒด
  • ์ด๋ฒคํŠธ ๊ฐ์ฒด๋Š” ํ•ด๋‹น ํƒ€์ž…์˜ ์ด๋ฒคํŠธ์— ๋Œ€ํ•œ ์ƒ์„ธ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์žˆ์Œ
  • ๋ชจ๋“  ์ด๋ฒคํŠธ ๊ฐ์ฒด๋Š” ์ด๋ฒคํŠธ ํƒ€์ž…์„ ๋‚˜ํƒ€๋‚ด๋Š” type ํ”„๋Ÿฌํผํ‹ฐ์™€ ์ด๋ฒคํŠธ ๋Œ€์ƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” target ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ
  • ์ด๋ฒคํŠธ ๊ฐ์ฒด๋Š” ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ
function sendit(e){     //e๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด
	console.log(e.target);  //์ด๋ฒคํŠธ ํƒ€๊ฒŸ(button)
	console.log(e.type);    //์ด๋ฒคํŠธ ํƒ€์ž…(onclick)
}
<input type = "button" onclick="sendit()" value="์™„๋ฃŒ">

์ด๋ฒคํŠธ ๋“ฑ๋ก

๊ฐ์ฒด.addEventListner(์ด๋ฒคํŠธ๋ช…, ์‹คํ–‰ํ• ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ, ์ด๋ฒคํŠธ์ „ํŒŒ๋ฐฉ์‹);

  • ์ด๋ฒคํŠธ ํƒ€์ž… : ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋กํ•  ์ด๋ฒคํŠธ ํƒ€์ž…
  • ์‹คํ–‰ํ•  ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ : ์ง€์ •๋œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰ํ•  ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ
  • ์ด๋ฒคํŠธ ์ „ํŒŒ ๋ฐฉ์‹ : false๋ฉด ๋ฒ„๋ธ”๋ง(bubbling) ๋ฐฉ์‹, true๋ฉด ์บก์ฒ˜๋ง(capturing) ๋ฐฉ์‹
    • ๋ฒ„๋ธ”๋ง ๋ฐฉ์‹:์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ, DOM ํŠธ๋ฆฌ๋ฅผ ๋”ฐ๋ผ ์œ„์ชฝ์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉฐ ์ „ํŒŒ๋˜๋Š” ๋ฐฉ์‹
      Document ๊ฐ์ฒด๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์—๋Š” Window ๊ฐ์ฒด๊นŒ์ง€ ๊ณ„์† ์ด์–ด์ง
    • ์บก์ฒ˜๋ง ๋ฐฉ์‹:์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ๊นŒ์ง€ DOM ํŠธ๋ฆฌ์˜ ์ตœ์ƒ์œ„๋ถ€ํ„ฐ ์•„๋ž˜์ชฝ์œผ๋กœ ๋‚ด๋ ค๊ฐ€๋ฉด ์ „ํŒŒ๋˜๋Š” ๋ฐฉ์‹
      ๋จผ์ € Window ๊ฐ์ฒด์˜ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์‹คํ–‰๋œ ํ›„, Document ๊ฐ์ฒด์— ๋“ฑ๋ก๋œ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์‹คํ–‰๋˜๊ณ , ๋˜๋‹ค์‹œ ๊ทธ ์ž์‹ ์š”์†Œ์— ๋“ฑ๋ก๋œ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ์‹คํ–‰
    • ์ด๋ฒคํŠธ ์ „ํŒŒ ์ทจ์†Œ ๋ฐฉ๋ฒ•:stopPropagation() ๋ฉ”์†Œ๋“œ๋‚˜ cancelBubble ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉ

์ด๋ฒคํŠธ ์ œ๊ฑฐ

๊ฐ์ฒด.removeEventListner(์ด๋ฒคํŠธํƒ€์ž…, ์ด๋ฒคํŠธ๋ฆฌ์Šค๋„ˆ);

๐Ÿณ location ๊ฐ์ฒด

  • ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ €์— ํ‘œ์‹œ๋œ HTML ๋ฌธ์„œ์˜ ์ฃผ์†Œ๋ฅผ ์–ป๊ฑฐ๋‚˜, ๋ธŒ๋ผ์šฐ์ €์— ์ƒˆ ๋ฌธ์„œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ ์‚ฌ์šฉ

location ๊ฐ์ฒด์˜ instance property

  • location.href: ํŽ˜์ด์ง€ URL ์ „์ฒด ์ •๋ณด๋ฅผ ๋ฆฌํ„ด ๋˜๋Š” URL์„ ์ง€์ •ํ•˜์—ฌ ํŽ˜์ด์ง€๋ฅผ ์ด๋™
  • location.protocol: ์ฝœ๋ก ์„ ํฌํ•จํ•˜๋Š” https, http, ftp ๋“ฑ ํ”„๋กœํ† ์ฝœ ์ •๋ณด๋ฅผ ๋ฆฌํ„ด
  • location.hostname: ํ˜ธ์ŠคํŠธ์˜ ์ด๋ฆ„ ๋ฆฌํ„ด
  • location.pathname: URL ๊ฒฝ๋กœ๋ถ€๋ถ„์˜ ์ •๋ณด๋ฅผ ๋ฆฌํ„ด
  • location.port: URL์˜ ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ ๋ฆฌํ„ด
  • location.host: ํ˜ธ์ŠคํŠธ์˜ ์ด๋ฆ„๊ณผ ํฌํŠธ๋ฒˆํ˜ธ๋ฅผ ๋ฆฌํ„ด

location ๊ฐ์ฒด์˜ instance method

  • location.reload(): ์ƒˆ๋กœ๊ณ ์นจ
  • location.assign(): ํ˜„์žฌ ์ฐฝ์— ์ง€์ •๋œ URL ์ฃผ์†Œ์— ์กด์žฌํ•˜๋Š” ๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
  • location.replace(): ์ƒˆ๋ฌธ์„œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์ „์— ํ˜„์žฌ ๋ฌธ์„œ๋ฅผ ๋ธŒ๋ผ์šฐ์ €์˜ ํžˆ์Šคํ† ๋ฆฌ์—์„œ ์ œ๊ฑฐํ•˜๊ณ , ๋ฌธ์„œ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

โฒ history ๊ฐ์ฒด

  • ๋ธŒ๋ผ์šฐ์ €์˜ ํžˆ์Šคํ† ๋ฆฌ ์ •๋ณด๋ฅผ ๋ฌธ์„œ์™€ ๋ฌธ์„œ ์ƒํƒœ ๋ชฉ๋ก์œผ๋กœ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด
  • ์‚ฌ์šฉ์ž์˜ ๊ฐœ์ธ ์ •๋ณด๋ฅผ ๋ณดํ˜ธํ•˜๊ธฐ ์œ„ํ•ด ์ด ๊ฐ์ฒด์˜ ๋Œ€๋ถ€๋ถ„์˜ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์„ ์ œํ•œ

history ๊ฐ์ฒด์˜ instance property

  • history.length: ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ €์˜ ํžˆ์Šคํ† ๋ฆฌ ๋ชฉ๋ก์˜ ๊ฐœ์ˆ˜

history ๊ฐ์ฒด์˜ instance method

  • history.back(): ๋’ค๋กœ ์ด๋™
  • history.forword(): ์•ž์œผ๋กœ ์ด๋™
  • history.go(0): ์ƒˆ๋กœ๊ณ ์นจ

๐Ÿ’ป screen ๊ฐ์ฒด

  • ์‚ฌ์šฉ์ž์˜ ๋””์Šคํ”Œ๋ ˆ์ด ํ™”๋ฉด์— ๋Œ€ํ•œ ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด

screen ๊ฐ์ฒด์˜ instance property

  • screen.width: ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ๋””์Šคํ”Œ๋ ˆ์ด ํ™”๋ฉด์˜ ๋„ˆ๋น„ (px)
  • screen.height: ํ˜„์žฌ ์‚ฌ์šฉ์ž์˜ ๋””์Šคํ”Œ๋ ˆ์ด ํ™”๋ฉด์˜ ๋†’์ด (px)

    (๋น„๊ต)
    window.outerWidth: ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ๋„ˆ๋น„ (px)
    window.outerHeight: ํ˜„์žฌ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ๋†’์ด (px)

  • screen.availWidth: ์šด์˜์ฒด์ œ์˜ ์ž‘์—… ํ‘œ์‹œ์ค„๊ณผ ๊ฐ™์€ ๊ณต๊ฐ„์„ ๋ชจ๋‘ ์ œ์™ธํ•œ ํ™”๋ฉด์˜ ๋„ˆ๋น„
  • screen.availHeight: ์šด์˜์ฒด์ œ์˜ ์ž‘์—… ํ‘œ์‹œ์ค„๊ณผ ๊ฐ™์€ ๊ณต๊ฐ„์„ ๋ชจ๋‘ ์ œ์™ธํ•œ ํ™”๋ฉด์˜ ๋†’์ด

๐Ÿงญ navigator ๊ฐ์ฒด

  • ๋ธŒ๋ผ์šฐ์ € ๊ณต๊ธ‰์ž ๋ฐ ๋ฒ„์ „ ์ •๋ณด๋“ฑ์„ ํฌํ•จํ•œ ๋ธŒ๋ผ์šฐ์ €์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด
  • geolocation: GPS ์ •๋ณด๋ฅผ ์ˆ˜์‹ ํ•˜๋Š” ํ”„๋Ÿฌํผํ‹ฐ
const success = function(loc){
	console.log(loc.coords.latitude);   //์œ„๋„
	console.log(loc.coords.longitude);     //๊ฒฝ๋„
}
const fail = function(msh){
	console.log(msg.code);
}
navigator.geolocation.getCurrentPosition(success, fail); 
//์„ฑ๊ณต์‹œ success(์•ž), ์‹คํŒจ์‹œ fail(๋’ค) ํ•จ์ˆ˜ ํ˜ธ์ถœ

๐ŸŒŒ window ๊ฐ์ฒด

window ๊ฐ์ฒด์˜ instance method

  • window.alter(): ๋Œ€ํ™”์ƒ์ž -> ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์•ผ ๋‹ค๋ฅธ ์ž‘์—… ๊ฐ€๋Šฅ
  • window.confirm(): ๋Œ€ํ™”์ƒ์ž -> ํ™•์ธ/์ทจ์†Œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์•ผ ๋‹ค๋ฅธ ์ž‘์—… ๊ฐ€๋Šฅ
  • window.prompt(): ๋Œ€ํ™”์ƒ์ž๋กœ ๋ฉ”์‹œ์ง€ ์ž…๋ ฅ๋ฐ›๊ธฐ
    -> ์ž…๋ ฅํ•˜๊ณ  ํ™•์ธ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ์•ผ ๋‹ค๋ฅธ ์ž‘์—… ๊ฐ€๋Šฅ
  • window.setTimeout(ํ˜ธ์ถœํ•  ํ•จ์ˆ˜, ์ง€์—ฐ์‹œ๊ฐ„):๋ช…์‹œ๋œ ์‹œ๊ฐ„์ด ์ง€๋‚œ ๋’ค์— ์ง€์ •๋œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
  • window.setInterval(ํ˜ธ์ถœํ•  ํ•จ์ˆ˜, ์ง€์—ฐ์‹œ๊ฐ„): ์ง€์ •๋œ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ๋งˆ๋‹ค ์ง€์ •๋œ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœ
  • window.clearTimeout(timeoutID): setTimeout() ๊ณ„ํš๋œ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ์ทจ์†Œ
  • window.clearInterval(timeoutID): setInterval() ๋ฐ˜๋ณต๋˜๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ์ทจ์†Œ

0๊ฐœ์˜ ๋Œ“๊ธ€