JavaScript ๋ด์ฉ์ ๋ฆฌ - 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>
์์ ๊ฐ์ด ๊ฐ์ ๋ฐฉ์์ผ๋ก ์ ๋ ฅํ๋ฉด ๋๋ค.
์ ์๋ํ๋๊ฒ์ ๋ณผ ์ ์๋ค.