<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<a id="target" href="https://github.com/keynene" title="keynene">keynene github</a>
<ul>
<li class="marked">HTML</li>
<li>CSS</li>
<li id="active" class="important current">JavaScript</li>
<ul>
<li>JavaScript Core</li>
<li class="marked">DOM</li>
<li class="marked">BOM</li>
</ul>
</ul>
</body>
※ 상기 html코드를 아래 예제들에 적용
for(var i=0; i<active.classList.length; i++){
console.log(active.classList[i]); // [0]important, [1]current 차례로 반환
}
/* document그룹 */
var list = document.getElementsByClassName('marked');
console.group('document');
for(var i=0; i<list.length; i++){
console.log(list[i].textContent); // HTML, DOM, BOM
}
console.groupEnd();
/* active그룹 */
console.group('active');
var active = document.getElementById('active');
var list = active.getElementsByClassName('marked');
for(var i=0; i<list.length; i++){
console.log(list[i].textContent); // DOM, BOM
}
console.groupEnd();
var t = document.getElementById('target');
console.log(t.getAttribute('href'));
t.setAttribute('href','https://github.com/keynene/javascript');
t.hasAttribute('title'); // title이라는 속성을 가지고 있는지 확인
var t = $('#target');
console.log(t.attr('href'));
t.attr('title','github.com/keynene'); // title추가 (setAttribute)
t.removeAttr('title'); //title삭제 (removeAttribute)
$(".marked","#active").css("background-color","red");
//active의 marked만 배경색 레드
$("#active .marked").css("background-color","red");
//위와 동일
$('#active').find('.marked').css("backgound-color","red");
//find 사용
$('#active').css('color','blue').find('.marked').css('background-color','red');
//active 글자색 블루, marked 배경색 레드