<html>
<head>
<script></script>
</head>
<body>
<button>Click Me!</button>
</body>
</html>
<html>
<head>
<script></script>
</head>
<body>
<button id="myButoon">Click Me!</button>
</body>
</html>
- document オブジェクトで指定する // ブラウザのみ
<html>
<head>
<script>
document.getElementById("myBUtton"); // idによって要素を取得する
</script>
</head>
<body>
<button id="myButoon">Click Me!</button>
</body>
</html>
- 上から実行されるので <script> タグを移動
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<script>
document.getElementById("myButton");
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<script>
const btn = document.getElementById("myButton");
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<script>
const btn = document.getElementById("myButton");
console.log(btn);
btn.addEventListener();
</script>
</body>
</html>
- ButtonというDOMオブジェクトを取得した
- Document Object Model と呼ばれる
- EventHanlerという関数を追加する
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<script>
const btn = document.getElementById("myButton");
console.log(btn);
btn.addEventListener();
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<script>
const btn = document.getElementById("myButton");
console.log(btn);
btn.addEventListener('click', function() { // 第1引数にイベント名(mouse_overなど) 第2引数に通常,無名関数を書く
});
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<script>
const btn = document.getElementById("myButton");
console.log(btn);
btn.addEventListener('click', () {
});
</script>
</body>
</html>
- 処理を書く // コンソールに clicked を出す
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<div id="div"/>
<script>
const btn = document.getElementById("myButton");
const div = document.getElementById("div");
console.log('btn');
btn.addEventListener('click', function() {
console.log('Clicked!')
});
</script>
</body>
</html>
<html>
<head>
</head>
<body>
<button id="myButoon">Click Me!</button>
<div id="div"/>
<script>
const btn = document.getElementById("myButton");
const div = document.getElementById("div");
console.log('btn');
btn.addEventListener('click', function() {
console.log('Clicked!')
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button id="myButton">Click Me!</button>
<div id="div"/>
<script>
const btn = document.getElementById("myButton");
const div = document.getElementById("div");
console.log(btn);
btn.addEventListener('click', () => {
console.log("Clicked!");
div.innerHTML = "Hello";
});
div.addEventListener('mouseover', () => {
div.style.color = 'red';
div.style.background = 'yellow';
});
</script>
</body>
</html>