HTML 구조
<div id="content"><b>box</b></div>
<div class="content">테스트1</div>
<div class="content">테스트2</div>
<div class="content">테스트3</div>
<button onclick="fnContentProp();">확인</button>
JavaScript 코드
innerHTML 값 가져오기
JavaScript:
console.log(document.getElementById('content').innerHTML);
jQuery
console.log($('#content').html());
innerHTML 값 수정하기
JavaScript:
const divList = document.getElementsByClassName('content'); // [div, div, div]
for(let divEl of divList){
divEl.innerHTML = '<b>javascript 방식으로 변경</b>';
}
jQuery
$('.content').html('<b>jQuery 방식으로 변경</b>');
innerHTML 값을 매번 다르게 수정하기
$('.content').html(function(index, html) {
return '안녕하세요 ' + index;
});<h2>style 관련 속성</h2>
<ul class="ott" style="background-color: lightblue;">
<li id="netflix"><a href="#">넷플릭스</a></li>
<li id="disney"><a href="#">디즈니플러스</a></li>
<li id="tving"><a id="tving-link" href="#">티빙</a></li>
<li id="wave"><a href="#">웨이브</a></li>
<li id="coupang"><span>쿠팡플레이</span></li>
</ul>
<button onclick="fnStyleProp();">확인</button>
JavaScript 코드
CSS 값 가져오기
console.log(document.querySelector('.ott').style.backgroundColor);
console.log($('.ott').css('backgroundColor'));
CSS 수정하기
const aList = document.querySelectorAll('.ott a'); // [a, a, a, a]
for(let aEl of aList){
aEl.style.textDecoration = 'none';
}
$('.ott a').css('fontSize', '32px')
.css('fontWeight', '900')
.css('color', 'red');
$('#coupang > span').css({
backgroundColor: 'pink',
color: 'green',
fontSize: '32px'
});
HTML 구조
아이디 : <input type="text" id="userId">
<button onclick="fnValueProp();">확인</button>
JavaScript 코드
value 값 가져오기
console.log(document.getElementById('userId').value);
console.log($('#userId').val());
value 값 수정하기
document.getElementById('userId').value = 'javascript 방식';
$('#userId').val('jQuery 방식').css('color', 'red');
HTML 구조
<style>
.blind {
display: none;
}
</style>
<div id="hello">hello world</div>
<button onclick="fnHide();">숨기기</button>
<button onclick="fnShow();">보이기</button>
<button onclick="fnToggle();">토글</button>
JavaScript 코드
$('#hello').addClass('blind');
$('#hello').removeClass('blind');
$('#hello').toggleClass('blind');
<img src="../assets/image/forest1.jpg" width="192px" id="img-forest"> <br>
<button onclick="fnAttribute();">확인</button>
JavaScript 코드
console.log($('#img-forest').attr('src'));
$('#img-forest').attr('src', '../assets/image/flower1.jpg')
.attr('height', '150px')
.css('border', '2px dashed pink');
$('#img-forest').removeAttr('width');