const test1 = document.getElementById("test1");
function getBoxText(){
console.log(test1.innerText);
}
function setBoxText(){
test1.innerText =
"innerText로 <br> 변경된 <b>내용입니다 © </b>";
}
function setBoxHTML(){
test2.innerHTML += "<ul><li>적용되나요??</li>"
+ "<li>됩니다!!</li></ul>";
}
<button onclick="alert('확인!!!!!!')">alert 확인</button>
const div1 = document.getElementById("div1");
const arr = document.getElementsByClassName("div2");
const hobbyArray = document.getElementsByName("hobby");
const arr = document.getElementsByTagName("li");
const bg = document.querySelector("#chatting-bg");
const arr = document.querySelectorAll("#css-div>div");
var : function level scope
-> 함수 내부에서 var로 선언하면 함수 안에서 쓸 수 있다
-> 단, if, for, while 등의 다른 {} 내부에 작성되어도
함수 안에 있는거면 무조건 function level
var : 변수, 변수명 중복 O(덮어쓰기), 함수 레벨 scope
let : 변수, 변수명 중복 X, 블록{} 레벨 scope
const : 상수, 변수명 중복 X, 블록{} 레벨 scope
1순위 : const
(JS는 특정 요소를 선택해서 사용하는 경우가 많아서 변수에 고정시켜둠)
2순위 : let
3순위 : var (ES6 이후 부터 사용 빈도가 많이 적어짐)
<button onclick="inlineEventModel(this)">인라인 이벤트 모델 확인</button>
<script>
function inlineEventModel(btn){
if(btn.style.backgroundColor != 'pink'){
btn.style.backgroundColor = 'pink';
} else{
btn.removeAttribute('style');
}
}
</script>
요소가 가지고 있는 이벤트 리스너 속성(on이벤트명)에
이벤트 핸들러(함수)를 직접 대입해서 연결하는 방법
인라인 이벤트 모델처럼 HTML에 작성하는 것이 아닌
script 태그 또는 js 파일에 작성
단점 : 한 요소의 같은 이벤트 리스너에 여러 이벤트 핸들러를 대입할 수 없다
<script>
const a = document.getElementById('test2-1');
a.onclick = function(){
alert('test2-1 클릭')
}
</script>
<div id="test3">클릭하면 크기가 늘어나요</div>
<script>
document.getElementById('test3').addEventListener("click", function(){
this.style.width = this.clientWidth + 10 + 'px';
});
</script>
<script>
document.getElementById('move').addEventListener('click', function(e){
e.preventDefault();
});
</script>