JavaScript
๋ฌธ์๊ฐ์ฒด
โข
์บ์คํ
: ๋ฌธ์๊ฐ์ฒด ์์ฑ
โข
์๋ฆฌ๋จผํธ ์ญ์ , ๋ถ๋ชจ๊ฐ์ฒด.removeChild(์์ ๊ฐ์ฒด) ๊ด๋ จ ๋ด์ฉ
<script>
// ์๋ฆฌ๋จผํธ ์ญ์
// ๋ถ๋ชจ๊ฐ์ฒด.removeChild(์์ ๊ฐ์ฒด)
document.addEventListener('DOMContentLoaded', () => {
// ๋ถ๋ชจ
// const div = document.querySelector('div')
// const h3 = document.querySelector('div > h3')
// div.removeChild(h3)
const delBtn = document.querySelector('div > input[type=button]')
// delBtn.onclick = (event) => {
// const div = document.querySelector('div')
// const h3 = document.querySelector('div > h3')
// div.removeChild(h3)
// }
delBtn.addEventListener('click', () => {
const div = document.querySelector('div')
const h3 = document.querySelector('div > h3')
div.removeChild(h3)
})
})
function delEl() {
const div = document.querySelector('div')
const h3 = document.querySelector('div > h3')
div.removeChild(h3)
}
</script>
<body>
<div>
<h3>์ญ์ ๋์</h3>
<p>๋ด์ฉ์ ์ค๋ช
ํฉ๋๋ค.</p>
<input type="button" style="color: red" value="์ญ์ ">
<!-- <input type="button" onClick="delEl()" style="color: red" value="์ญ์ "> - ๊ณ ์ ๋ฐฉ์ -->
</div>
</body>
JavaScript
๋ณต์ฌ
โข
ํค๋ณด๋ ์ด๋ฒคํธ ์ ์ฉ
<script>
// ํค๋ณด๋ ์ด๋ฒคํธ
// keydown : ํค๊ฐ ๋๋ฆด๋ ๋ฐ์์ ํจ
// keypress : ํค๊ฐ ์
๋ ฅ๋ ๋ x
// keyup: ํค๊ฐ ๋จ์ด์ง ๋ m
document.addEventListener('DOMContentLoaded', () => {
const textarea = document.querySelector('textarea')
textarea.addEventListener('keyup', (event) => {
const str_len = event.currentTarget.value.length // currentTarget: ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ๋์์ ๊ฐ๋ฆฌํด
const span_cnt = document.getElementById('cnt') // ๊ธ์ ์ ์ ํ
span_cnt.textContent = str_len
if (str_len > 100) {
alert('100๊ธ์ ๊น์ง ์์ฑ์ด ๊ฐ๋ฅํฉ๋๋ค.')
}
})
</script>
<body>
<!-- ๊ธ์ ์ -->
<h1><span id="cnt"></span> / 100</h1>
<textarea cols="20" rows="10">
</textarea>
</body>
JavaScript
๋ณต์ฌ
โข
์ ํ ํญ๋ชฉ ์ ์ฉ
<script>
const select = document.querySelector('Select')
const h2_print = document.querySelector('h2.print')
select.addEventListener('change', (event) => {
const options = event.currentTarget.options
const index = event.currentTarget.options.selectedIndex
h2_print.textContent = `์ ํ ํญ๋ชฉ : ${options[index].textContent}`
})
</script>
<body>
<h2 class="print"></h2>
<!-- ์ ํ, ์์น๊ฐ์ ๊ฐ์ง๊ณ ๊ตฌ๋ถ -->
<select>
<option>HTML5</option>
<option>CSS</option>
<option>javascript</option>
<option>jquery</option>
<option>react.js</option>
</select>
</body>
JavaScript
๋ณต์ฌ
โข
radio button ์ ์ฉ
<script>
const h3_output = document.getElementById('output')
const radios = document.querySelectorAll('[name=lang]')
// ๋ชจ๋ radio์ ์ด๋ฒคํธ ์ฐ๊ฒฐ
radios.forEach((radio) => {
// ์ด๋ฒคํธ ์ฐ๊ฒฐ
radio.addEventListener('change', (event) => {
const current = event.currentTarget.radio
if (current.checked) {
h3_output.textContent = `์ ํ ์ธ์ด : ${current.value.checked}`
}
})
})
</script>
<body>
<h3>๊ด์ฌ ์๋ ์ธ์ด๋ฅผ ์ ํํ์ธ์!</h3>
<input type="radio" name="lang" value="JAVA" checked> <span>JAVA</span>
<input type="radio" name="lang" value="HTML5"> <span>HTML5</span>
<input type="radio" name="lang" value="Javascript"> <span>Javascript</span>
<h3 id="output">์ ํ ์ธ์ด : JAVA</h3>
</body>
JavaScript
๋ณต์ฌ
โข
todo list
โฆ
์นด์ดํธ ๋ณ์๋ฅผ ํ์ฉํ์ฌ ์ญ์ ๊ธฐ๋ฅ ์ ์ฉ
โช
์ญ์ ๋์ โ div
โช
const key = keyCount++
โฆ
checkbox, button ์ด๋ฒคํธ ์ฒ๋ฆฌ
โฆ
ํ ์ผ ์ถ๊ฐ ํ ์ปค์๊ฐ ํ
์คํธ๋ฐ์ค์ ์์นํ๋๋ก focus() ์ฌ์ฉ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js_todo</title>
<script>
document.addEventListener('DOMContentLoaded', () => {
// ์บ์คํ
const input_newTodo = document.querySelector('#newTodo')
const add_Btn = document.querySelector('#addBtn')
const todoList = document.querySelector('#todoList')
// ์นด์ดํธ ๋ณ์
let keyCount = 0
add_Btn.addEventListener('click', () => {
if (input_newTodo.value.trim() === '') {
alert('ํ ์ผ์ ์
๋ ฅํด์ฃผ์ธ์!!!')
return
}
// alert(input_newTodo.value.trim())
// div > checkbox, span, button
const div_new = document.createElement('div')
const key = keyCount++
div_new.setAttribute('data-key', key)
// checkbox
const checkbox = document.createElement('input')
checkbox.type = 'checkbox'
// checkbox ์ด๋ฒคํธ ์ฒ๋ฆฌ
checkbox.addEventListener('change', (event) => {
div_new.style.textDecoration = event.currentTarget.checked ? 'line-through' : ''
})
// span
const span_todostr = document.createElement('span')
span_todostr.textContent = input_newTodo.value
// button
const button_del = document.createElement('button')
button_del.textContent = 'ํ ์ผ ์ญ์ '
// button ์ด๋ฒคํธ ์ฒ๋ฆฌ
button_del.addEventListener('click', (event) => {
// ์ญ์ ๋์ - > div
const div_del = document.querySelector(`[data-key="${key}"]`)
todoList.removeChild(div_del)
})
div_new.appendChild(checkbox)
div_new.appendChild(span_todostr)
div_new.appendChild(button_del)
todoList.appendChild(div_new)
input_newTodo.value = ''
input_newTodo.focus() // ํ ์ผ ์ถ๊ฐ ํ ์ปค์๊ฐ ํ
์คํธ๋ฐ์ค์ ์์นํจ
})
})
</script>
</head>
<body>
<!-- ์ฌ์ฉ์๊ฐ ํ ์ผ์ ์
๋ ฅํ๋ฉด ๋ฆฌ์คํธ์ ์ถ๊ฐ -->
<h1>Todo List</h1>
ํ ์ผ ์ถ๊ฐ :
<input type="text" id="newTodo">
<button id="addBtn">ํ ์ผ ์ถ๊ฐ</button>
<hr>
<div id="todoList"></div>
</body>
</html>
JavaScript
๋ณต์ฌ
โข
class
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>js_class</title>
<script>
// ๊ฐ์ฒด => {}
// ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ธฐ ์ํ ํ =>
// new ํ() => ํ์์ ์ ์๋ ๋ณ์๋ ํจ์๋ก ์ด๋ฃจ์ด์ง ๊ฐ์ฒด ์์ฑ
// class ํด๋์ค ์ด๋ฆ { ๋ณ์, ํจ์ } => new ํด๋์ค์ด๋ฆ()
let obj_student = {
name: '์ํฅ๋ฏผ',
sno: '21145712',
major: '์ปดํจํฐ'
}
class Student {
constructor(name, sno, major) {
this.name = name
this.sno = sno
this.major = major
}
}
let student1 = new Student();
console.log(typeof (student1)) // object
const students = [
new Student('son', '111', 'computer'),
// => {name: '', sno: '', major: ''}
new Student('son', '123', 'computer'),
new Student('son', '123', 'computer')
]
console.log(students)
// students.push( new Student('son', '111', 'computer'))
// local_storage: ๋ฐ์ดํฐ์ ๋ธ๋ผ์ฐ์ ๋ฅผ ์ ์ฅํ๊ณ ๊ด๋ฆฌํด์ฃผ๋ ๊ณณ
// getItem : ํค๊ฐ์ผ๋ก ๋ถ๋ฌ์ด
// setItem : local_storage์ ์ ์ฅ
let saved_data = localStorage.getItem('members')
console.log('saved_data', saved_data)
//if (saved_data == null) {
localStorage.setItem('members', 'members List')
localStorage.removeItem('members')
//}
</script>
</head>
<body>
</body>
</html>
JavaScript
๋ณต์ฌ




