How to change span value in jquery

青灯夜游
Release: 2022-12-16 17:29:51
Original
3951 people have browsed it

Change steps: 1. Use the jquery selector to obtain the specified span element, the syntax "$("selector")"; 2. Use the text() or html() function to modify the content value of the specified element object , syntax "element object.text("new content")" or "element object.html("new content")".

How to change span value in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

In jquery, you can use the jquery selector and text() (or html()) function to change the content value of span.

Implementation idea:

  • Get the specified span element object.

  • Modify the content value of the specified element object.

Implementation steps:

Step 1. Use jquery selector to obtain the specified span element

$("选择器")
Copy after login
  • Will return the jquery object containing the specified span element

Step 2. Use the text() or html() function to modify the content of the specified element object The value

  • text() can set the text content of the element, which can be changed by simply setting the text content to the new value.

  • html() can set or return content that contains text and HTML tags.

span元素对象.text("新内容")
span元素对象.html("新内容")
Copy after login

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<script src="js/jquery-3.6.1.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					$("#sp1").text("text()修改的新内容值")
					$("#sp2").html(&#39;html()修改的新内容值&#39;);
					$("#sp3").html(&#39;<b>html()修改的新内容值</b>&#39;);
				});
			});
		</script>
	</head>
	<body>
		<button>改变span元素的内容</button><br><br>
		<span id="sp1">span元素旧内容。</span><br><br>
		<span id="sp2">span元素旧内容。</span><br><br>
		<span id="sp3">span元素旧内容。</span><br><br>
	</body>
</html>
Copy after login

How to change span value in jquery

##Extended knowledge: Comparison between html() and text()

html() gets all the content inside the element, while text() gets only the text content.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-3.6.1.min.js"></script>
		<script>
			$(function () {
            var strHtml = $("p").html();
            var strText = $("p").text();
            $("#txt1").val(strHtml);
            $("#txt2").val(strText);
        })
    </script>
	</head>
	<body>
		<p><strong style="color:hotpink">PHP中文网</strong></p>
		html()是:<input id="txt1" type="text" /><br />
		text()是:<input id="txt2" type="text" />
	</body>
</html>
Copy after login

How to change span value in jquery

The difference between the two methods html() and text() can be clearly compared from the following table.

HTML codehtml()text()
PHP Chinese websitePHP Chinese website##
PHP Chinese website
PHP中文网
(empty string)[Recommended learning: jQuery video tutorial
##PHP中文网
,

web front-end development

The above is the detailed content of How to change span value in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!