初心者向け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 prevとnext前と後の要素取得する

jQuery prevとnext前と後の要素取得する jQuery prev前の要素取得する prevは英語の略語で previous(以前の) 等です。 prevは指定した要素の前の要素を取得します …

no image

jQuery textメソッドとhtmlの違い 文字列を変更する

jQuery textメソッド 文字列を変更する jQuery textメソッド 文字列を変更する textメソッドは要素のtextを変更することができます。 $(“セレクタ” …

no image

jQuery clickクリックイベントの書き方

clickクリックイベントの書き方 clickクリックイベントの書き方 クリックイベントの書き方の前にイベントの書き方を解説します。 サンプル $(function(){ $(“セレクタ& …

no image

疑似クラスと疑似要素

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

no image

jQuery animate要素をアニメーションさせる

jQuery animate要素をアニメーションさせる jQuery animate要素をアニメーションさせる animateの英語の意味は 生命を吹き込む,活気づける、活発にする、励ます、動画にする …