WEB/HTML+CSS+JS

JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 2

kite707 2021. 1. 2.

๋ฐ์ดํ„ฐ ํƒ€์ž… - ๋ฌธ์ž์—ด๊ณผ ์ˆซ์ž

์ˆซ์ž(Number) 

-์—ฐ์‚ฐ ๊ฐ€๋Šฅ

+ : ๋”ํ•˜๊ธฐ

- : ๋นผ๊ธฐ

* : ๊ณฑํ•˜๊ธฐ

/ : ๋‚˜๋ˆ„๊ธฐ

 

๋ฌธ์ž์—ด(String)

-๋”ฐ์˜ดํ‘œ๋กœ ๋ฌถ์–ด์ค˜์•ผํ•จ

-str.length  -> ๊ธ€์ž์ˆ˜ ๋ฆฌํ„ด

-str.toUpperCase()  -> ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ฐ”๊ฟ”์คŒ

-str.indexOf('์ฐพ์„ ๋ฌธ์ž')  -> ์ฐพ๊ณ ์ž ํ•˜๋Š” ๋ฌธ์ž๊ฐ€ ๋ช‡๋ฒˆ์งธ ์ž๋ฆฌ์— ์žˆ๋Š”์ง€ ์•Œ๋ ค์คŒ

์˜ˆ์‹œ

'Hello world'.indexOf('O') //๋Œ€๋ฌธ์ž O
// -1๋ฆฌํ„ด (๊ฐ’์„ ์ฐพ์„ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ)
'Hello world'.indexOf('o') //์†Œ๋ฌธ์ž o
// 4๋ฆฌํ„ด (์ฒซ๋ฒˆ์งธ ์ž๋ฆฌ๋ฅผ 0์œผ๋กœ ํ•ด์„œ ์†Œ๋ฌธ์žo๊ฐ€ 4๋ฒˆ์งธ ์ž๋ฆฌ์— ์žˆ๊ธฐ ๋•Œ๋ฌธ)

์•„๋ž˜ ๋งํฌ์—์„œ ๋” ๋งŽ์€ ์ž๋ฃŒ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String

 

String - JavaScript | MDN

String ์ „์—ญ ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด(๋ฌธ์ž์˜ ๋‚˜์—ด)์˜ ์ƒ์„ฑ์ž์ž…๋‹ˆ๋‹ค.๋ฌธ์ž์—ด ๋ฆฌํ„ฐ๋Ÿด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•์‹์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. 'string text' "string text" "ไธญๆ–‡ español Deutsch English เคฆเฅ‡เคตเคจเคพเค—เคฐเฅ€ ุงู„ุนุฑุจูŠุฉ português

developer.mozilla.org

๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ๋ฒ•

var ๋ณ€์ˆ˜์ด๋ฆ„ = ๊ฐ’

 

 

์›น ํŽ˜์ด์ง€ ์ œ์–ดํ•˜๊ธฐ

๊ฐ•์˜๋ฅผ ์‹œ์ž‘ํ•˜๋ฉฐ ๋ดค๋˜ ํ™”๋ฉด์„ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ๋‹คํฌ๋ชจ๋“œ/ํ™”์ดํŠธ๋ชจ๋“œ๋กœ ๋ณ€ํ•˜๋„๋ก ํ•˜๋Š” ์›น ํŽ˜์ด์ง€๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณด์ž. ์‹œ์ž‘ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB1 - JavaScript</title>
  </head>
  <body>
    <h1><a href="index.html">WEB</a></h1>
      <ol>
        <li><a href="1.html">HTML</a></li>
        <li><a href="2.html">CSS</a></li>
        <li><a href="3.html">JavaScript</a></li>
      </ol>
      <h2>JavaScript</h2>
      <p>
        JavaScript (/หˆdส’ษ‘หvษ™หŒskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
      </p>
  </body>
</html>

์ฒ˜์Œ์—๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์•„๋ฌด ๋ฒ„ํŠผ์ด ์—†๋Š” ์ƒํƒœ์ด๋‹ค.

๋ฐฐ์šด ๊ฒƒ์„ ํ™œ์šฉํ•ด ์šฐ์„  ๋ฒ„ํŠผ์„ ๋งŒ๋“ค๊ณ  ํด๋ฆญ์„ ํ–ˆ์„ ์‹œ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒ๋˜๋„๋ก ํ•˜์ž.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB1 - JavaScript</title>
  </head>
  <body>
    <h1><a href="index.html">WEB</a></h1>
    <input type="button" value="night" onclick="">
    <input type="button" value="day" onclick="">
      <ol>
        <li><a href="1.html">HTML</a></li>
        <li><a href="2.html">CSS</a></li>
        <li><a href="3.html">JavaScript</a></li>
      </ol>
      <h2>JavaScript</h2>
      <p>
        JavaScript (/หˆdส’ษ‘หvษ™หŒskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
      </p>
  </body>
</html>

 

๋ฒ„ํŠผ์ด ์ƒ๊ฒผ๋‹ค

๊ทธ๋ฆฌ๊ณ  ์ œ์–ดํ•  ํƒœ๊ทธ๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ๊ตฌ๊ธ€์— "javascript select tag by css selector"๋ผ๊ณ  ์ž…๋ ฅํ•œ๋‹ค.

๊ทธ๋ ‡๊ฒŒ ํ•ด์„œ ์ฐพ์€ ๋งํฌ

https://developer.mozilla.org/ko/docs/Web/API/Document/querySelector

 

Document.querySelector() - Web API | MDN

Document.querySelector()๋Š” ์ œ๊ณตํ•œ ์„ ํƒ์ž ๋˜๋Š” ์„ ํƒ์ž ๋ญ‰์น˜์™€ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์„œ ๋‚ด ์ฒซ ๋ฒˆ์งธ Element๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ผ์น˜ํ•˜๋Š” ์š”์†Œ๊ฐ€ ์—†์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ฐธ๊ณ : ํƒ์ƒ‰์€ ๊นŠ์ด์šฐ์„ depth-first ์ „์œ„pre-order

developer.mozilla.org

๋˜ element์˜ ์Šคํƒ€์ผ์„ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด ๊ฒ€์ƒ‰์„ ํ•˜์ž. ๊ฒ€์ƒ‰์–ด๋Š” "javascript element style" ์ด๋‹ค. 

https://developer.mozilla.org/ko/docs/Web/API/HTMLElement/style

 

element.style - Web API | MDN

HTMLElement.style ์†์„ฑ์€ ์š”์†Œ์˜ ์ธ๋ผ์ธ ์Šคํƒ€์ผ์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ ‘๊ทผ์ž๋กœ์„œ๋Š” ์š”์†Œ์˜ ์ธ๋ผ์ธ style ์†์„ฑ์ด ํฌํ•จํ•œ CSS ์„ ์–ธ์„ ๋‹ด์€ CSSStyleDeclaration ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค elt.s

developer.mozilla.org

์ด๋Ÿฐ ๋งํฌ๋ฅผ ์ฐพ์•˜๋‹ค. ์ฐพ์€ ์ •๋ณด์˜ ๋‚ด์šฉ์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

์ด์ œ ๋ฒ„ํŠผ์˜ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐ”๊พผ๋‹ค.

    <input type="button" value="night" onclick="
    document.querySelector('body').style.backgroundColor='black';
    ">

๊ทธ๋ฆฌ๊ณ  ์ด ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋ฉด 

์ด๋ ‡๊ฒŒ ๊ฒ€์ •์ƒ‰์œผ๋กœ ๋ฐ”๋€๋‹ค. ๋˜ ๊ธ€์ž์ƒ‰์„ ๋ฐ”๊ฟ€ ๋•Œ๋Š” .colorํ•˜๊ณ  ์ƒ‰์„ ์ž…๋ ฅํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฐ๊ณผํ™”๋ฉด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB1 - JavaScript</title>
  </head>
  <body>
    <h1><a href="index.html">WEB</a></h1>
    <input type="button" value="night" onclick="
    document.querySelector('body').style.backgroundColor='black'; //๋ฐฐ๊ฒฝ์„ ๋ฐ”๊ฟ€๋•Œ
    document.querySelector('body').style.color='white'; //๊ธ€์ž ์ƒ‰์„ ๋ฐ”๊ฟ€ ๋•Œ
    ">
    <input type="button" value="day" onclick="">
      <ol>
        <li><a href="1.html">HTML</a></li>
        <li><a href="2.html">CSS</a></li>
        <li><a href="3.html">JavaScript</a></li>
      </ol>
      <h2>JavaScript</h2>
      <p>
        JavaScript (/หˆdส’ษ‘หvษ™หŒskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
      </p>
  </body>
</html>

์œ„๋Š” ์ „์ฒด ์ฝ”๋“œ์ด๋‹ค. ์ด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ day๋ฒ„ํŠผ๋„ ํ™”์ดํŠธ๋ชจ๋“œ๋กœ ๋ฐ”๋€Œ๋„๋ก ํ•ด๋ณด์ž.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>WEB1 - JavaScript</title>
  </head>
  <body>
    <h1><a href="index.html">WEB</a></h1>
    <input type="button" value="night" onclick="
    document.querySelector('body').style.backgroundColor='black'; //๋ฐฐ๊ฒฝ์„ ๋ฐ”๊ฟ€๋•Œ
    document.querySelector('body').style.color='white'; //๊ธ€์ž ์ƒ‰์„ ๋ฐ”๊ฟ€ ๋•Œ
    ">
    <input type="button" value="day" onclick="
    document.querySelector('body').style.backgroundColor='white'; //๋ฐฐ๊ฒฝ์„ ๋ฐ”๊ฟ€๋•Œ
    document.querySelector('body').style.color='black'; //๊ธ€์ž ์ƒ‰์„ ๋ฐ”๊ฟ€ ๋•Œ
    ">
      <ol>
        <li><a href="1.html">HTML</a></li>
        <li><a href="2.html">CSS</a></li>
        <li><a href="3.html">JavaScript</a></li>
      </ol>
      <h2>JavaScript</h2>
      <p>
        JavaScript (/หˆdส’ษ‘หvษ™หŒskrษชpt/[6]), often abbreviated as JS, is a high-level, dynamic, weakly typed, prototype-based, multi-paradigm, and interpreted programming language. Alongside HTML and CSS, JavaScript is one of the three core technologies of World Wide Web content production. It is used to make webpages interactive and provide online programs, including video games. The majority of websites employ it, and all modern web browsers support it without the need for plug-ins by means of a built-in JavaScript engine. Each of the many JavaScript engines represent a different implementation of JavaScript, all based on the ECMAScript specification, with some engines not supporting the spec fully, and with many engines supporting additional features beyond ECMA.
      </p>
  </body>
</html>

์œ„์™€ ๊ฐ™์ด ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

์ž˜ ์ž‘๋™ํ•˜๋Š”๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

'WEB > HTML+CSS+JS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 4  (0) 2021.01.04
JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 3  (0) 2021.01.03
JavaScript ๋‚ด์šฉ์ •๋ฆฌ - 1  (0) 2021.01.01
CSS ๋‚ด์šฉ์ •๋ฆฌ - ์„ ํƒ์ž  (0) 2020.12.31
CSS ๋‚ด์šฉ์ •๋ฆฌ - 4  (0) 2020.12.31

๋Œ“๊ธ€