Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 코딩테스트
- 그럼에도 불구하고
- 변수
- TypeScript
- coding
- 코드업
- java
- 자바
- cleancode
- media query
- JS
- 반응형 페이지
- react
- webpack
- max-width
- git
- HTML
- node
- redux
- @media
- JavaScript
- 프론트엔드
- CSS
- Servlet
- 그럼에도불구하고
- frontend
- node.js
- github
- react-router-dom
- 자바문제풀이
Archives
- Today
- Total
그럼에도 불구하고
day / night mode 구현하기 본문
day/ night mode를 만들어보자
[ 조건 ]
1. day / night mode를 만들 수 있는 버튼은 1개만 사용하자
2. 버튼을 누르면 body의 backgroundColor과 fontColor를 해당 모드에 맞게 바꾸자
3. 해당 모드에 맞는 버튼의 value를 변경하자
See the Pen Untitled by zenghyun (@zenghyun) on CodePen.
code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
input { width:100px; height: 80px; font-size: 24px;}
</style>
</head>
<body>
<h1>day mode / night mode</h1>
<input type="button" value="night" id="visual" onclick="change()">
<script>
let visual = document.getElementById('visual');
function change() {
if ( visual.value === 'night' ){
document.querySelector('body').style.backgroundColor = 'black';
document.querySelector('body').style.color = 'white';
visual.setAttribute('value','day');
}
else {
document.querySelector('body').style.backgroundColor = 'white';
document.querySelector('body').style.color = 'black';
visual.setAttribute('value','night');
}
}
</script>
</body>
</html>
핵심
[ element.setAttribute() ]
Syntax : element.setAttribute(name, value)
Parameters
Parameter | 설명 |
name | 필수요건 속성의 이름 |
value | 필수요건 새로운 속성의 값 |
※ 주의사항
요소에 값이 있는 style 속성을 추가할 수 있지만 style 속성의 다른 속성을 덮어쓸 수 있으므로 권장하지 않습니다.
권장 사용 방법
// 이렇게 사용하지 말자
element.setAttribute("style", "background-color:red;");
// 이렇게 사용하자
element.style.backgroundColor = "red";
ref: https://www.w3schools.com/jsref/met_element_setattribute.asp
HTML DOM Element setAttribute() Method
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'JavaScript > Function implementation' 카테고리의 다른 글
숫자 반환하기 (0) | 2022.12.29 |
---|---|
정규 표현식으로 특정 문자 검색하기 (1) | 2022.12.28 |
지역 검색 구현하기 (1) | 2022.12.24 |
제곱 함수 구하기 (0) | 2022.12.19 |
day / night mode ver_2 (0) | 2022.12.16 |
Comments