初心者向け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

疑似クラスと疑似要素

疑似クラスと疑似要素 疑似クラス 疑似クラスとは疑似的に処理をセレクタに追加するものです。 :をつけます。 疑似クラス一覧 :active :any :checked :default :dir() …

no image

jQuery eq要素のインデックスを取得する

jQuery eq要素のインデックスを取得する jQuery eq要素のインデックスを取得する eqは指定した要素のインデックスを指定する。 サンプル $(“セレクタ”).eq …

no image

jQuery 子孫セレクタとユニバーサルセレクタと複数

jQuery 子孫セレクタとユニバーサルセレクタと複数 jQuery 子孫セレクタ 子孫セレクタとは 要素には親要素と子要素があります。 ulだと子はliですね。 それとdivの中にp要素を入れ子にし …

no image

jQuery first-childとlast-childの勘違い

jQuery first-childとlast-child jQuery first-child first-childは一番最初の子要素を指定します。 $(“要素:first-child …

no image

jQuery parentsとparentの違い祖先を指定する

jQuery parentsとparenの違い祖先を指定する jQuery parentsとparenの違い祖先を指定する parentsとparentの違いはparent親は親要素全てに適応できます …