1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

javascriptの基礎

Last updated at Posted at 2020-04-18

onload呼び出し

ページ表示するときにjavascriptを実行したい時に

document.addEventListener("DOMContentLoaded", function () {
  console.log("hello, world!");
});
const func = () => {
  console.log("hello, world!");
};
func();

DOMの作成

<body> 内に <div class="container">container</div> を作りたい時に

const divContainer = document.createElement("div");
divContainer.classList.add("container");
divContainer.textContent = "container";
document.body.appendChild(divContainer);

DOMの取得

html の class="container" 要素を取得したい時に

const container = document.querySelector(".container");
const containers = document.querySelectorAll(".container");

イベントの追加

html の class="button" にクリックイベントを付けたい時に

const button = document.querySelector(".button");
button.addEventListener("click", function() {
  console.log("click!");
});

const buttons = document.querySelectorAll(".button");
buttons.forEach(button => {
  button.addEventListener("click", function() {
    console.log("click!");
  });
});
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?