jQuery を JS に書き換えたい
脱 jQuery するぞい
対象物の指定
id 属性での指定
jQuery ... $('#id')
JS ... document.getElementById('id')
class 属性での指定
jQuery ... $('.class')
JS ... document.getElementsByClassName('class')
Element ではなく Elements(複数形)
要素での指定
jQuery ... $('div')
JS ... document.getElementsByTagName('div')
Element ではなく Elements(複数形)
参考
マウスイベント
jQuery
code:js
$('#id').on('click', function () {
// 内容...
});
JS
code:js
document.getElementById('id').addEventListener('click', function() {
// 内容...
});
↑古い IE で対応していない書き方なので注意
参考
クリックした要素の子要素を取得
JS
code:js
function (event) {
console.log(event.target.children);
}
event のあとに target が必要
コツ:console.log(event) を見てみる
code:js
function (event) {
console.log(event);
}
Console の内容
https://gyazo.com/00cd8eecf5cf08f04a390af34c467788