初心者向けjQuery入門

初心者向けjQuery入門講座です

未分類

jQuery attrとremoveAttr属性の取得と設定と削除

投稿日:2017年8月15日 更新日:

jQuery attrとremoveAttr属性の取得と設定と削除

attrはattributeの略ですね。
英語で属性という意味です。

jQuery attr属性の設定する

サンプル
$(“セレクタ”).attr(“属性”,”値”)

サンプル

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“a”).attr(“href”,”http://jquery.navi-ch.net/”)

 

});
</script>
<title>

</title>
</head>
<body>
<a href=”#”>初心者向けjQuery入門</a>
<h3 class=”red”>red</h3>
<h3 class=”Green”>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>

 

</body>
</html>

jQuery attr属性の取得する

サンプル
$(“セレクタ”).attr(“属性名”)

サンプル

<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“a”).attr(“href”,”http://jquery.navi-ch.net/”)
$(“h3”).html($(“a”).attr(“href”))

 

});
</script>

&nbsp;
<div></div>
<a href=”#”>初心者向けjQuery入門</a>
<h3 class=”red”>red</h3>
<h3 class=”Green”>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>

実行結果は
初心者向けjQuery入門
http://jquery.navi-ch.net/

http://jquery.navi-ch.net/

http://jquery.navi-ch.net/

http://jquery.navi-ch.net/

h3をhtmolの引数にattrで属性を取得してます。

jQuery removeAttr 属性を削除する

removeAttrは属性を削除します。

サンプル

$(“セレクタ”).removeAttr(“削除する属性名”)

サンプル

<html>
<head>
<script src=”jquery-3.2.1.js”></script>

<script>
$(document).ready(function(){

$(“a”).attr(“href”,”http://jquery.navi-ch.net/”)

 

$(“a”).removeAttr(“href”)

});
</script>
<title>

</title>
</head>
<body>
<div></div>
<a href=”#”>初心者向けjQuery入門</a>
<h3 class=”red”>red</h3>
<h3 class=”Green”>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>

 

</body>
</html>

実行結果は

初心者向けjQuery入門
red

Green

yellow

Blue

初心者向けjQuery入門のリンクがなくなっています。

スポンサードリンク




スポンサードリンク




-未分類
-

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

jQuery prevALLとnextALL前と後の兄弟要素を全て取得する

jQuery prevALLとnextALL前と後の兄弟要素を全て取得する jQuery prevALL前の兄弟要素を全て取得する prevALLメソッドは指定した前の兄弟要素を全て取得します。 サン …

no image

jQuery removeとemptyの違い

removeとemptyの違い removeとemptyの違い removeとemptyの違い。 removeは要素をDOMから削除します。 emptyは要素を子ノードから削除します。 remove …

no image

jQuery:notセレクタの使い方

jQuery jQuery:notセレクタの使い方 jQuery jQuery:notセレクタの使い方 :notセレクタは条件を指定した以外の要素を指定します。 :not(h2) 使用方法はセレクタに …

no image

jQuery 複数のcssのプロパティの設定の方法

jQuery 複数のcssのプロパティの設定の方法 jQuery 複数のcssのプロパティの設定の方法 jQueryでcssを複数設定の方法。 メソッドチェーンで書くとこんな感じになります。 $(&# …

no image

jQuery widthとheight要素の横と縦を設定する

jQuery widthとheight要素の横と縦を設定する jQuery width 要素のサイズの横幅を設定するにはwidthメソッドを使用します。 $(“セレクタ”).w …