0
0

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.

[JS] DOM(Document Object Model)

Last updated at Posted at 2021-03-28

DOMとは

DOM(Document Object Model)とは
HTMLをJavaScriptから操作できるようにしたインターフェイスです
JavaScriptからはDOM APIを通じてHTMLの情報を取得・変更します
情報の取得・変更・イベントの登録などができます

documentはHTML情報が格納されているオブジェクトです
プロパティ・メソッドを通してHTMLにアクセスして情報を参照・変更したりできます
これがDOM API、もしくはDOM インターフェイスと呼ばれます

取得

querySelectorでDOMへアクセスできる

idで指定する

Image from Gyazo

classで指定する
Image from Gyazo

タグで指定する
Image from Gyazo

例えばliが複数あっても最初の要素のみ取得されます
[Image from Gyazo]
(https://gyazo.com/5d4dad165de97286831e9764d3f83704)

全てのliを取得したい場合、querySelectorAllを使います
Image from Gyazo
NodeListは配列に近いオブジェクト

変更

Image from Gyazo
h1の文字列JavaScriptを→ 定数h1に代入

Image from Gyazo
JavaScript → Arigatoに変更
Arigato → Makotoni Arigatoに変更

Image from Gyazo

Image from Gyazo


classList.addで

Image from Gyazo

h1にclass="over"が付与されます
Image from Gyazo

他にも
classList.removeで削除、
classList.toggleで追加したり削除したりできます


このようにしてあげるとli要素にstyleを適用できます

Image from Gyazo

Image from Gyazo

forEachで回してあげると全てのli要素にstyleを適用できます
Image from Gyazo

Image from Gyazo

イベント

const btn = document.querySelector('#btn');
const hello = function() {
  alert('hello');
};

btn.addEventListener('click', hello);

Image from Gyazo


const btn = document.querySelector('#btn');
const hello = function() {
  this.style.color = 'green';
  console.log(this);
};

btn.addEventListener('click', hello);

Image from Gyazo

thisの中身はこうなっています
Image from Gyazo


addEventListenerでは複数の関数を登録することができます

const btn = document.querySelector('#btn');
const h1 = document.querySelector('h1');

function changeColor() {
  h1.style.color = 'green';
};

function changeBgColor() {
  h1.style.backgroundColor = 'yellow';
};

btn.addEventListener('click', changeColor);
btn.addEventListener('click', changeBgColor);

Image from Gyazo


DOMContentLoaded

HTMLをブラウザが解釈して、DOMツリーを作成し終わったタイミングで発火するイベント

load

画像・動画・stylesheetなど、全てのコンテンツをダウンロードし終えた時に発火するイベント


trimでHTML文の前後のスペースを取り除く

document.addEventListener('DOMContentLoaded', function() {
  const el = document.querySelector('.animate-title');
  console.log(el.innerHTML);
});

Image from Gyazo

.trimをつけるとこうなる

document.addEventListener('DOMContentLoaded', function() {
  const el = document.querySelector('.animate-title');
  console.log(el.innerHTML.trim());
});

Image from Gyazo


文字列にもループが使えるので、このようにfor文で回すと

document.addEventListener('DOMContentLoaded', function() {
  const el = document.querySelector('.animate-title');
  const str = el.innerHTML.trim();

  for(let c of str) {
    console.log(`<span class="char">${c}</span>`);
  }
});

Image from Gyazo

1文字ずつspan classが付く

replaceメソッド

HTMLは半角スペースを無視する仕様なので、
挿入したい場合はreplaceメソッドで半角スペースを&nbsp;に置換してあげます

document.addEventListener('DOMContentLoaded', function() {
  const el = document.querySelector('.animate-title');
  const str = el.innerHTML.trim();
  let concatStr = '';

  for(let c of str) {
    c = c.replace(' ', '&nbsp;');
    concatStr += `<span class="char">${c}</span>`;
  }
  el.innerHTML = concatStr;
});

Image from Gyazo

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?