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>
リンクになっていますね。
リンクになっているので文字は青がなります。
リンク先は設定してません。