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.

jQueryでよく使うメソッドまとめ

Last updated at Posted at 2020-03-09

メソッドを使うとき一番最初に使うであろう簡単なメソッドをご紹介

親子要素を特定する

find


<div class="parent">
  <div class="child">こんにちは</div>
</div>
<script>
  //子要素を取得できる
  $('.parent').find('.child').text();
</script>

parents


<div class="parent">
  <div class="child">こんにちは</div>
</div>
<script>
  //親以上の要素を取得できる(要素指定)
  //親はparentメソッドを使用
  $('.child').parents('.parent').html();
</script>

要素の設定値を取得、設定する

attr


<div class="hello" name='hello'>こんにちは</div>
<script>
  //要素の属性を取得できる
  $('.hello').attr('name');
  //要素の属性を設定できる
  $('.hello').attr('name','goodMorning');
</script>

val


<input type="text" class="hello"value="こんにちは">
<script>
  //inputタグの入力値を取得できる
  $('.hello').val();
  //inputタグの入力値を設定できる
  $('.hello').val('こんばんは');

</script>

要素に追加する

on

<div class="hello">こんにちは</div>
<script>
  //イベントを登録できる
  $('.hello').on('click',function(){
    //クリックした時の処理をここに書く
  });
</script>

css

<div class="hello">こんにちは</div>
<script>
  //CSSを反映する
  $('.hello').css("color", "red");
</script>

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?