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】val(),prev(),on()

Posted at

jQueryについて学習内容を備忘録としてまとめます。

val()

html要素にval()を実行することでvalue要素を取得することができます。


//html
<button id="btn" value="a">ボタン</button>

//jQueryの処理
var btn = $('#btn').val();
console.log( btn );

//実行結果
a

実行結果にある通り、htmlのvalue要素であるaを表示しています。

prev()

対象要素の直前にあるHTML要素を取得することができます。


//html
<div class="test">
	<p>testtest</p>
	<a>リンク</a>
</div>
//jQueryの処理
var result = $('a').prev().text();
console.log(result);

//実行結果
testtest

このようにaの直前にあるtesttestが実行結果として表示されます。

on()

さまざまなイベント処理を記述するためのメソッドになります。
たとえば、マウス操作やフォームから何か送信されることが該当になります。


//html
<button>ボタン</button>
//jQueryの処理
 $('button').on('click', function() {
    
        console.log('クリックされました!');
    
    })
//実行結果
クリックされました

ボタンをクリックするとjQueryが稼働してクリックされました!と表示されます。

参考URL

https://www.sejuku.net/blog/45297
https://www.sejuku.net/blog/58952
https://www.sejuku.net/blog/38774

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?