[jq-3.html]
text メソッドは要素の中の文字列のみ付与,取得する事ができます.
<body>
<h1>最初の見出し</h1>
<input type="button" value="rewrite" onclick="changeText()">
<input type="button" value="getText" onclick="geteText()">
</body>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
function changeText(){
$('h1').text('書き換えた見出し');
}
function geteText(){
var text = $('h1').text();
console.log(text);
}
</script>
()の中に引数があれば付与,なければ取得に自動的に切り替わります
コメントを残す