初心者向け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 beforeとafterの違いと要素の追加

jQuery beforeとafterの違いと要素の追加 jQuery beforeとafterの違い beforeとafterは基本的な動作は似てます。 共通してるのは要素を追加することです。 違う …

no image

jQuery wrapとunwrap親要素を追加と削除

jQuery wrapとunwrap親要素を追加と削除 jQuery wrap 親要素を追加 wrapは親要素を追加します サンプル $(“セレクタ”).wrap(&#8216 …

no image

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

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

no image

jQuery hoverマウスと乗せたり外した時動作するイベント

jQuery hoverマウスと乗せたり外した時動作するイベント jQuery hoverマウスと乗せたり外した時動作するイベント hoverはマウスを乗せたり外したりしたときに起動するイベントです。 …

no image

jQuery addclassクラスを追加する方法

jQuery addclassクラスを追加する方法 jQuery addclassクラスを追加する方法 addclassクラスは要素にクラスを設定できます。 どういうことかというこクラスのタブにhtm …