Home  >  Article  >  Web Front-end  >  What is the method to get element text in jquery

What is the method to get element text in jquery

青灯夜游
青灯夜游Original
2022-03-16 15:19:076215browse

The method to get the text of an element in jquery is "text()". The text() method can get or set the text content of the selected element (HTML tags will be deleted). The syntax for getting the text content is "$(selector).text()".

What is the method to get element text in jquery

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

The method to get the text of an element in jquery is "text()".

text() method sets or returns the text content of the selected element.

When the text() method is used to return a value, it returns the combined text content of all matching elements (HTML tags will be removed).

Syntax:

$(selector).text()    //获取文本内容
$(selector).text(content)   //设置文本内容

The text() method has the same effect as the innerText attribute, except that text() is implemented in jQuery, while innerText is implemented in JavaScript. Way.

Example:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.min.js"></script>
		<script>
			$(document).ready(function() {
				$("button").click(function() {
					alert($("p").text());
				});
			});
		</script>
	</head>
	<body>

		<button>返回所有p元素的文本内容</button>
		<p>这是一个段落。</p>
		<p>这是<b>另一个</b>段落。</p>

	</body>
</html>

What is the method to get element text in jquery

Extended knowledge: Comparison of html() and text()

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<script src="js/jquery-1.10.2.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>

What is the method to get element text in jquery

As can be seen from this example, html() obtains all the content inside the element, while text() obtains only the text content. Additionally, the val() method is used to get and set the value of a form element.

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

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

, web front-end

The above is the detailed content of What is the method to get element text in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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