初心者向けjQuery入門

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

未分類

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

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

jQuery textメソッド 文字列を変更する

jQuery textメソッド 文字列を変更する

textメソッドは要素のtextを変更することができます。

$(“セレクタ”).text(“変更したい文字列”)

サンプル

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

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

$(“.red”).text(“pink”)
$(“.red”).css({“font-size”:”72px”,”font-style”:”italic”,”background-color”:”red”})

});
</script>

</title>
</head>
<body>
<div>
<h3 class=”red”>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>

</body>
</html>

テキストが変更されてpinkになっているはずです。
簡単ですね。

わかりやすいようにクラスredにイタリックと72pxと背景赤設定してます。

jQuery htmlメソッド htmlを変更する

htmlメソッドは要素のhtmlを変更することができます。

$(“セレクタ”).html(‘htmlのタグ’)

サンプル

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

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

$(“.red”).html(‘<a href=”#”>pink</a>’)
$(“.red”).css({“font-size”:”72px”,”font-style”:”italic”,”background-color”:”red”})

});
</script>
<title>

</title>
</head>
<body>
<div>
<h3 class=”red”>red</h3>
<h3>Green</h3>
<h3>yellow</h3>
<h3>Blue</h3>
</div>

</body>
</html>

リンクになっていますね。

リンクになっているので文字は青がなります。

リンク先は設定してません。

 

 

スポンサードリンク




スポンサードリンク




-未分類
-,

執筆者:


comment

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

関連記事

no image

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

jQuery attrとremoveAttr属性の取得と設定と削除 attrはattributeの略ですね。 英語で属性という意味です。 jQuery attr属性の設定する サンプル $(&#822 …

no image

jQuery nth-child偶数と奇数と二番目指定する方法

jQuery nth-child偶数と奇数nth-last-childとnth first child jQuery nth-child nth-childは何番目の要素を指定するセレクタです。 nt …

no image

jQuery slideUpとslideDownとslideToggle

jQuery slideUpとslideDownとslideToggle jQuery slideUp slideUpはスライドをアップします。 要素が上に上がっていき非表示になります。 サンプル $ …

no image

jQuery チルダ~の間接セレクタと兄弟セレクタ

jQuery チルダ~の間接セレクタと兄弟セレクタ jQuery チルダ~の間接セレクタと兄弟セレクタ 間接セレクタはチルダを使用します。 兄弟とは親が同じだということですね。 動作は指定した兄弟の以 …

no image

jQuery removeClassクラスを削除する方法

jQuery removeClassクラスを削除する方法 jQuery removeClassクラスを削除する方法 htmlで設定したクラスを削除します。 cssで設定したクラスでも削除はできます。 …