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 5 years have passed since last update.

jqueryでDOM操作

Posted at

要素の取得

通常のjsだと

document.getElementById('id');
document.querySelector('要素名');

取得するものがidなのかクラスなのか、要素なのか、属性なのかで文が変わってくるが、jqueryなら

$('#id');
$('.class');
$('要素');
$('要素[属性= "値"]');

のように短くシンプルな記述で取得できる。

他の記述も短く書けて

# javascript
btn = document.getElementById('btn');
btn.classList.add('red');

# jquery
$('#btn').addClass('red');

のように短く記述できる。

またプロパティやバリューの値の取得もでき、

$('form').prop('checked');
# チェックボックスのチェックが入ったらつくプロパティ

$('form').attr('value');
# value="値"の値をとる

他には

$('form').on('submit',function(e){処理内容});

でformの送信ボタンが押されたら処理を行う。みたいなのも簡単にかける。

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?