document.querySelector("h1").innerText = "welcome";
document.querySelector("h1").textContent = "welcome";
$h1.className = "yellow";
.yellow{
color: yellow;
}
class="h1"
-> .yellow넣음 -> class="yellow"
//css
.yellowgreen {
color: yellowgreen;
transition: color 0.5s ease;
}
.tomato {
color: tomato;
transition: color 0.5s ease;
}
.font {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
let $h1 = document.querySelector(".h1");
const onClick = function () {
const clickYellowgreen = "yellowgreen";
const clickTomato = "tomato";
if ($h1.classList.contains(clickYellowgreen)) {
$h1.classList.remove(clickYellowgreen);
$h1.classList.add(clickTomato);
} else {
$h1.classList.remove(clickTomato);
$h1.classList.add(clickYellowgreen);
}
};
$h1.addEventListener("click", onClick);
$h1.classList.contains(clickYellowgreen)
$h1.classList.remove(clickYellowgreen);
$h1.classList.add(clickYellowgreen);
$h1.classList.toggle("tomato");
text.style.color = "green";
function random(number) {
return Math.floor(Math.random() * (number + 1));
}
const onClick = function () {
$h1.style.color =
"rgb(" + random(255) + "," + random(255) + "," + random(255) + ")";
};
//클릭 감지
$h1.addEventListener("click", onClick);
//마우스가 올라가면 감지
$h1.addEventListener("mouseenter", onClick);
//마우스가 벗어나면 감지
$h1.addEventListener("mouseleave", onClick);
const windowResize = function () {
document.body.style.backgroundColor = "tomato";
};
//윈도우 창의 크기가 변하는 것을 감지한다.
window.addEventListener("resize", windowResize);
//윈도우 창에서 복사(command+c 감지)
window.addEventListener("copy", windowResize);
//인터넷 연결 감지
window.addEventListener("online", windowResize);
//인터넷 연결 끊김 감지
window.addEventListener("offline", windowResize);
//js로 색바꾸기
const onClick = function () {
if (text.style.color !== "yellowgreen") {
text.style.color = "yellowgreen";
} else {
text.style.color = "tomato";
}
};
text.addEventListener("click", onClick);
//class를 넣어서 색 바꾸기
const onClick = function () {
const currentColor = $h1.className;
let newColor;
if (currentColor !== "yellowgreen") {
newColor = "yellowgreen";
} else {
newColor = "tomato";
}
$h1.className = newColor;
};
$h1.addEventListener("click", onClick);
//css에서 class만들기
.yellowgreen {
color: yellowgreen;
transition: color 0.5s ease;
}
.tomato {
color: tomato;
transition: color 0.5s ease;
}
//js로 색바꾸기 -> 코드리뷰
const onClick = function () {
const currentColor = text.style.color;
let newColor;
const changeYellowgreen = "yellowgreen";
const changeTomato = "tomato";
if (currentColor !== changeYellowgreen) {
newColor = changeYellowgreen;
} else {
newColor = changeTomato;
}
text.style.color = newColor;
};
text.style.color !== "yellowgreen";
에서 text.color.color은 현재 색깔이 옐로우그린이냐? 판단하는 것이기 때문에 currentColortext.style.color = "tomato";
에서 색깔을 tomato로 바꾸는 것이기 때문에 newColor로 바꿈8)참고
.yellowgreen {
color: yellowgreen;
transition: color 0.5s ease;
}
.tomato {
color: tomato;
transition: color 0.5s ease;
}