๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • What would life be If we had no courage to attemp anything?
๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง

๐“๐จ๐๐š๐ฒ ๐ˆ ๐‹๐ž๐š๐ซ๐ง 2022.04.27.์ˆ˜

by DevIseo 2022. 5. 2.

Today I Learn 220427

์˜ค๋Š˜์€ JavaScript์˜ Dom ์กฐ์ž‘์— ๋Œ€ํ•ด ๋ฐฐ์› ๋‹ค.

์žŠ๊ณ ์žˆ๋˜ html&css ๊ฐœ๋…์„ ๋„์ง‘์–ด์˜ค๋ ค๋‹ˆ ์ข€ ํ—ท๊ฐˆ๋ ธ๋‹ค.

์ด๋ฒˆ์ฃผ ๊ธˆ์š”์ผ์— ๋‹ค์‹œ ํ•œ ๋ฒˆ ์“ฑ ํ›‘์–ด๋ด์•ผ๊ฒ ๋‹ค.

์˜ค๋Š˜ ์›Œํฌ์ƒต ๊ณผ์ œ๋ฅผ ํ•˜๋Š”๋ฐ ์•ฝ๊ฐ„ ๋‚ด์šฉ์ด ์ดํ•ด๊ฐ€ ์•ˆ๋˜์–ด์„œ ๊ณผ์ œ ํ’€์ดํ•˜๋Š”๋ฐ ์–ด๋ ค์›€์ด ์žˆ์—ˆ๋‹ค.

์†์„ฑ๊ฐ’ ์ง€์ •ํ•ด์ฃผ๋Š”๊ฑด ์ž˜ ํ–ˆ๋Š”๋ฐ, tag๋ฅผ ์—ฐ๊ฒฐํ•ด ์ค„ ๋•Œ ์•ฝ๊ฐ„ ํ—ท๊ฐˆ๋ ธ๊ณ 

    // div#app ์š”์†Œ ์„ ํƒ
    const divTag = document.querySelector('#app')
    
    // h1 ํƒœ๊ทธ๋ฅผ createElement ๋กœ ์ƒ์„ฑ
    const h1Tag = document.createElement('h1')
    // ์ƒ์„ฑํ•œ h1ํƒœ๊ทธ์˜ ๋‚ด์šฉ์„ '์˜ค๋Š˜์˜ Todo' ๋กœ ์„ค์ •
    h1Tag.innerText = '์˜ค๋Š˜์˜ Todo'
    divTag.append(h1Tag)
    // ul, li ํƒœ๊ทธ๋“ค์„ ์ƒ์„ฑ ๋ฐ ๋‚ด์šฉ ์ถ”๊ฐ€
    const ulTag = document.createElement('ul')
    const liTag1 = document.createElement('li')
    liTag1.innerText = 'homeworkํ•˜๊ธฐ'
    const liTag2 = document.createElement('li')
    liTag2.innerText = 'workshopํ•˜๊ธฐ'
    ulTag.append(liTag1, liTag2)
    divTag.append(ulTag)

๋ถ€๋ชจ ํด๋ž˜์Šค๋ฅผ ์ž˜ ์—ฐ๊ฒฐํ•ด ์ถœ๋ ฅ์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹ค์Œ ๊ณผ์ œ๋„ ์•ฝ๊ฐ„ ์ดํ•ด๊ฐ€ ์•ˆ๋˜์—ˆ๋Š”๋ฐ, ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ถˆ๋Ÿฌ์™€์„œ ๋„ฃ์–ด์ค˜์•ผ ํ–ˆ๋Š”๋ฐ

    const cardsSection = document.querySelector('#cardsSection')

    function createCard(title, content) {
      // ์—ฌ๊ธฐ์— ์นด๋“œ๋ฅผ ์ž‘์„ฑํ•˜์‹œ์˜ค.
      const Article = document.createElement("article")
      Article.setAttribute("class","col-4")
      const Card = document.createElement("div")
      Card.setAttribute("class","card m-1")
      const CardBody = document.createElement("div")
      CardBody.setAttribute("class","card-body")
      const CardBodyTitle = document.createElement("h5")
      CardBodyTitle.setAttribute("class","card-title")
      CardBodyTitle.innerText = title
      const CardBodyText = document.createElement("p")
      CardBodyText.setAttribute("class","card-text")
      CardBodyText.innerText = content

      Article.append(Card)
      Card.append(CardBody)
      CardBody.append(CardBodyTitle,CardBodyText)
      return Article

์ด๋Ÿฐ์‹์œผ๋กœ ๋„ฃ์–ด์คฌ๋Š”๋ฐ, ์•„๋ž˜ ๋ฐฉ๋ฒ•์ฒ˜๋Ÿผ ๋„ฃ์–ด์ค„ ์ˆ˜ ๋„ ์žˆ์„๊ฑฐ ๊ฐ™๋‹ค.

  const articleAtts = cardsSection.querySelector("article").getAttribute("class")
  const newArticle = document.createElement("article")
  newArticle.setAttribute("class",articleAtts)

๋Œ“๊ธ€