LoginSignup
1
1

More than 3 years have passed since last update.

JS thisの使い方について

Last updated at Posted at 2019-11-30

thisについて
thisはイベントの中でそのイベントが起こった要素を取得する事ができる。
よく聞く文言だがよく分からなかったので調べてみた

例を出すとわかりやすく

<div>
 <ul>
  <li>text1</li>
  <li>text2</li>
  <li>text3</li>
 </ul>
</div>
$function(){
 $('li').click(function(){
  $(this).css('color','red');
  });
});

このように指定した場合、仮にtext1をクリックした際にthisの中にはli要素のtext1が取得されtext1の文字の色が赤に変化する

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