Javascript復習
36786 ワード
📌 html動的制御 <body>
<ul class="list">
<li id="one">1</li>
<li id="two">2</li>
<li id="three">3</li>
</ul>
<script>
var one = document.querySelector('#one')
var list = document.querySelector('.list')
list.innerHTML = '<h1>안녕</h1>'
</script>
<body>
<ul class="list">
<li id="one">1</li>
<li id="two">2</li>
<li id="three">3</li>
</ul>
<script>
var one = document.querySelector('#one')
var list = document.querySelector('.list')
list.innerHTML = '<h1>안녕</h1>'
</script>
🔥 createElement, AppendChild
var listAdd = document.createElement('li')
listAdd.innerHTML="안녕"
list.appendChild(listAdd)
🔥 getAttribute
<body>
<ul class="list">
<li id="one">1</li>
<li id="two">2</li>
<li id="three">3</li>
<a id="google" href="https://google.com">google</a>
</ul>
<script>
var one = document.querySelector('#one')
var list = document.querySelector('.list')
var google = document.querySelector('#google')
console.log(google.getAttribute('href'))
</script>
📌 Event
🔥 addEventListener
<body>
<div id="title">임얼쑤</div>
<button class="btn">click</button>
<script>
var btn = document.querySelector('.btn')
var mainTitle = document.querySelector('#title')
function btnClicklHandler(){
mainTitle.style.color='dodgerblue'
}
btn.addEventListener('click',btnClicklHandler)
</script>
</body>
<body>
<div id="title">임얼쑤</div>
<button class="btn">click</button>
<script>
var btn = document.querySelector('.btn')
var mainTitle = document.querySelector('#title')
function btnClicklHandler(){
mainTitle.style.color='dodgerblue'
}
btn.addEventListener('click',btnClicklHandler)
</script>
</body>
🔥 青->オレンジに変更
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{font-size : 3rem;}
.btn{font-size : 10rem}
.menu-link{
background: dodgerblue;
}
.menu-active{
background: orange;
}
</style>
</head>
<body>
<nav class="menu">
<a href="#" class="menu-link">menu 1</a>
<a href="#" class="menu-link">menu 2</a>
<a href="#" class="menu-link">menu 3</a>
</nav>
<script>
function clickMenuHandler(){
this.classList.add('menu-active')
}
var menuLinks = document.querySelectorAll('.menu-link')
for(i in menuLinks){
menuLinks[i].addEventListener('click',clickMenuHandler)
}
</script>
</body>
🔥 e.target / e.currentTarget
<body>
<nav class="menu">
<a href="#" class="menu-link">menu 1</a>
<a href="#" class="menu-link">menu 2</a>
<a href="#" class="menu-link">menu 3</a>
</nav>
<script>
var menu = document.querySelector('.menu')
function click(e){
console.log(e.target)
console.log(e.currentTarget)
}
menu.addEventListener('click',click)
</script>
</body>
💰 e.target=現在クリック
💰 e.currentTarget = this
🔥 コード#コード#
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{font-size : 3rem;}
.btn{font-size : 10rem}
.menu-link{
background: dodgerblue;
}
.menu-active{
background: orange;
}
</style>
</head>
<body>
<nav class="menu">
<a href="#" class="menu-link">menu 1</a>
<a href="#" class="menu-link">menu 2</a>
<a href="#" class="menu-link">menu 3</a>
</nav>
<script>
var menu = document.querySelector('.menu')
var currentMenu;
function active(item){
item.classList.add('menu-active')
currentMenu = item;
}
function inActive(item){
item.classList.remove('menu-active')
}
function handler(e){
if(currentMenu){
inActive(currentMenu)
}
active(e.target)
}
menu.addEventListener('click',handler)
</script>
</body>
</html>
Reference
この問題について(Javascript復習), 我々は、より多くの情報をここで見つけました https://velog.io/@camel-man-ims/Javascript-복습テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol