조건문 if
(Conditional statements)
1. if 조건문
if(조건){
실행될 코드;
}
1-1. Boolean(true/false)
if(true){
alert('javascript');
}
1-2. else
if(true){
alert(1);
} else {
alert(2);
}
if문의 조건이 true라면 if의 중괄호 구간이 실행
if문의 조건이 false라면 else 이후의 중괄호 구간이 실행
1-3. if~else 조건문의 true/false 예시
<h2>IF-ture</h2>
<script>
document.write("1<br>"); //1
if(true){
document.write("2<br>"); //2
}else{
document.write("3<br>"); //if가 ture이므로 실행X
}
document.write("4<br>"); //4
</script>

1. value 값 가져오기
var value = document.getElementByid(id).value;
2. 예시) 주/야간모드 토글버튼 만들기
제어할 태그 선택하기의 주/야간모드에서 발전
<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';
}
">
Last updated
Was this helpful?