初心者向け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 first-childとlast-childの勘違い

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

no image

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

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

no image

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

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

no image

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

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

no image

jQuery showとhideで要素を表示と非表示する方法

jQuery showとhideで要素を表示と非表示する方法 jQuery show 要素を表示する方法 showの英語の意味は見せるです。 サンプル $(“セレクタ”).sh …