attr()-jQueryメソッド

jQuery

attr()メソッド構文1

構文

attr(属性名);

attr()メソッド構文1の詳細

処理内容

HTML要素の属性値を取得する。
※jQueryオブジェクトは返らない。

引数

属性名

attr()メソッド構文2

構文attr(‘属性名’,値);

attr()メソッド構文2の詳細

処理内容

HTML要素の属性値を取得し、設定する。
※jQueryオブジェクトが返る。

引数

引数1:属性名
引数2:値

 

attr()メソッドを使ったコード例

HTML

<button class="olive button1">button1 olive</button>
</br>
<button class="olive button2">button2 olive</button>

CSS

button{
  width:300px;
  height:60px;
}
.olive{
  background-color:olive;
}
.orange{
  background-color:orange;
}

jQuery

var $arg = $('button.button1').attr('class');
document.write($arg + ';');
var $arg2 = $('button.button2').attr('class','orange');
document.write($arg2[0]);

コード

See the Pen
attr()
by roymoy (@roymoy)
on CodePen.

JavaScript・jQueryのおすすめ

おすすめ

 

スポンサードリンク

スポンサードリンク