그리고 자바랑 CSS랑은 달라서, querySelectorAll('a') 로 선택을 하시면 안됩ㄴ비다.
querySelectorAll('a')는 a태그가 달린 모든 걸 선택한다는게 아니고, 모든걸 배열로 가져온다는 뜻입니다.
배열은 나중에 나오겠지만,
쉽게말하면 querySelectorAll('a')[0] 이 첫번째 a태그, querySelectorAll('a')[1] 이 두번째 a태그... 이런식으로 지정이 되는거라, for문으로 처음부터 끝까지 돌면서 변경해줘야하는것 같아요.
<!DOCTYPE html>
<html>
<head>
<title> WEB1 - JavaScript</title>
<meta charset="utf-8">
</head>
<body>
<h1><a href="index.html">WEB</a></h1>
<input id="night_day" type="button" value="night" onclick="
if(document.querySelector('#night_day').value === 'night'){
document.querySelector('body').style.backgroundColor = 'black';
document.querySelector('body').style.color = 'white';
document.querySelector('#night_day').value = 'day';
} else {
document.querySelector('body').style.backgroundColor = 'white';
document.querySelector('body').style.color = 'black';
document.querySelector('#night_day').value = 'night';
}
">
<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>
우리가 입력한 코드를 넣고 웹페이지를 열었을 때
웹페이지의 속성값이 night가 되고 onclick을 했을 때
if를 타고 문서인의 선택자 아이디 값이 value와 같기 때문에
body의 backgroundColor를 블랙으로 넣을 것이다.
body의 text color를 화이트로 넣을 것이다.
그리고 문서인의 선택자 아이디 값이 value를 day로 넣을 것이다.
또 한 번 onclick을 했을 때
if를 타고 문서인의 선택자 아이디 값이 night에서 day로 변환되어 있기 때문에
조건이 맞지 않아
else를 읽고 값을 night로 다시 변환시킨다.