2024.04.09
클래스를 이용한 문서 객체 탐색
<script>
window.onload = function(){
const foo = document.getElementsByClassName('matched');
let output = '[class명이 matched인 모든 태그의 text 출력]\\n';
for(let i=0; i<foo.length; i++){
output += foo[i].innerHTML + '\\n';
}
alert(output);
const foo2 = document.getElementById('foo');
const fooMatched = foo2.getElementsByClassName('matched');
let output2 = '[id가 foo인 태그의 하위 태그 중 class명이 matched인 모든 태그의 text명을 출력]\\n';
for(let i=0; i<fooMatched.length;i++){
output2 += fooMatched[i].innerHTML + '\\n';
}
alert(output2);
const fooMatched2 = foo2.getElementsByClassName('matched unmatched')
let output3 = '[id가 foo인 태그의 하위 태그 중 class명이 matched unmatched인 모든 태그의 text 출력]\\n';
for(let i=0; i<fooMatched2.length; i++){
output3 += fooMatched2[i].innerHTML + '\\n';
}
alert(output3);
};
</script>
QuerySelector
<script>
window.onload = function(){
let header1 = document.querySelector('#header_1');
let header2 = document.querySelector('#header_2');
header1.innerHTML = '서울';
header2.innerHTML = '부산';
let input1 = document.querySelector('#my_form #input_1');
input1.value = '홍길동';
let element = document.querySelector('li');
element.style.backgroundColor='Yellow';
}
</script>
QuerySelectorAll
<script>
const elems = document.querySelectorAll('ul > li');
for(let i=0; i<elems.length; i++){
document.write(elems[i].innerHTML + '<br>');
}
document.write('-------------------<br>');
elems.forEach(function(element){
element.style.color = 'blue';
});
</script>
태그 생성
<script>
window.onload = function(){
const header = document.createElement('h1');
const textNode = document.createTextNode('Hello DOM');
header.appendChild(textNode);
document.body.appendChild(header);
}
</script>
문서 객체의 속성 지정
<script>
window.onload = function(){
const img = document.createElement('img');
img.src = '../files/landscape.jpg';
img.width = 500;
img.height = 350;
document.body.appendChild(img);
}
</script>
문서 객체의 innerHTML 속성 사용
<script>
window.onload = function(){
let output='';
output += '<ul>';
output += '<li>JavaSfcript</li>';
output += '<li>jQuery</li>';
output += '<li>Ajax</li>';
output += '</ul>';
document.body.textContent = output;
}
</script>
문서 객체 제거
<script>
window.onload = function(){
const willRemove = document.getElementById('will_remove');
setTimeout(function(){
document.body.innerHTML = '';
},3000);
};
</script>
문서 객체의 스타일 처리
<script>
window.onload = function(){
const header = document.getElementById('header');
header.style.border = '2px solid black';
header.style.color = 'orange';
header.style.fontFamily = 'Helvetica';
header.style.backgroundColor = 'yellow';
};
</script>
인라인 이벤트 처리
<input type="button" value="이동" onclick="location.href='https://www.naver.com'">
<input type="button" value="확인" onclick="alert('클릭'); alert('Click');">
인라인 이벤트 처리 - 함수 이용
<script>
function whenClick(){
alert('클릭!');
}
</script>
<body>
<div id="header" onclick="whenClick();">클릭</div>
</body>
스크립트(메뉴 만들기)
<script>
window.onload=function(){
const menu = document.getElementsByTagName('li');
for(let i=0; i<menu.length; i++){
menu[i].onmouseover=function(){
this.style.backgroundColor='#FF0000';
};
menu[i].onmouseout=function(){
this.style.backgroundColor='#FF8888';
}
}
}
</script>
스크립트(사진에 마우스 갖다대면 사진 사이즈 키우기)
<script>
window.onload = function(){
const bigImg = document.getElementById('bigImg');
const alink = document.getElementsByTagName('a');
for(let i=0; i<alink.length; i++){
alink[i].onmouseover = function(){
bigImg.style.display='';
bigImg.src = this.href;
bigImg.widht = 500;
bigImg.height = 350;
};
alink[i].onmouseout = function(){
bigImg.style.display='none';
};
alink[i].onclick = function(){
return false;
};
}
};
</script>
키보드 입력 시 이벤트
<script>
window.onload=function(){
document.getElementById('content').onkeyup = function(){
let inputLength = this.value.length;
let remain = 150 - inputLength;
let letter = document.getElementById('letter');
letter.innerHTML = remain;
};
}
</script>
간단한 회원가입 폼
<script>
window.onload = function(){
const form = document.getElementById('register_form');
form.onsubmit = function(){
const name = document.getElementById('name');
if(name.value.trim()==''){
alert('이름을 입력하세요!');
name.value = '';
name.focus();
return false;
}
const id = document.getElementById('id');
if(id.value.trim()==''){
alert('아이디를 입력하세요!');
id.value = '';
id.focus();
return false;
}
const password = document.getElementById('password');
if(password.value.trim()==''){
alert('비밀번호를 입력하세요!');
password.value = '';
password.focus();
return false;
}
const zipcode = document.getElementById('zipcode');
if(zipcode.value.trim()=='' || zipcode.value.length!=5){
alert('우편번호는 5자리를 꼭 입력하세요!');
zipcode.value='';
zipcode.focus();
return false;
}
};
};
</script>
<body>
<form id="register_form" action="a.jsp" method="post">
<fieldset>
<legend>회원가입</legend>
<ul>
<li>
<label for="name">이름</label>
<input type="text" name="name" id="name" size="10" maxlength="10">
</li>
<li>
<label for="id">아이디</label>
<input type="text" name="id" id="id" size="10" maxlength="10">
</li>
<li>
<label for="password">비밀번호</label>
<input type="password" name="password" id="password" size="10" maxlength="10">
</li>
<li>
<label for="zipcode">우편번호</label>
<input type="text" name="zipcode" id="zipcode" size="10" maxlength="5">
</li>
</ul>
<div align="center">
<input type="submit" value="전송">
</div>
</fieldset>
</form>
</body>
간단한 갤러리 만들기
<style>
*{
margin:0;
padding:0;
}
#galleryWrap{
width:520px;
margin: 0 auto;
text-align: center;
}
#galleryWrap img{
vertical-align:middle;
}
img#prev, img#next{
cursor:pointer;
}
</style>
<script>
window.onload = function(){
let num = 1;
let gallery = document.getElementById('gallery');
document.getElementById('prev').onclick=function(){
num--;
if(num<1){
num=7;
}
gallery.src='../files/img'+num+'.jpg';
};
document.getElementById('next').onclick=function(){
num++;
if(num>7){
num=1;
}
gallery.src='../files/img'+num+'.jpg';
};
};
</script>
<body>
<div id="galleryWrap">
<h1>이미지 넘기기</h1>
<p>
<img src="../files/left_btn.png" alt="이전 그림 보기" id="prev">
<img src="../files/img1.jpg" alt="갤러리 그림" id="gallery">
<img src="../files/right_btn.png" alt="다음 그림 보기" id="next">
</p>
</div>
</body>