text()
从匹配元素集合中得到每个元素的文本信息,并将其合并在一起返回一个String。不能对input元素使用。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
var str = $("p:first").text();
$("p:last").html(str);
});
</script>
<style>
p { color:blue; margin:8px; }
b { color:red; }
</style>
</head>
<body>
<p><b>Test</b> Paragraph.</p>
<p></p>
</body>
</html>
var str = $("p:first").text();
获取匹配p元素集合中第一个p元素的文本内容。以下是获取文本信息
Test Paragraph
text(val)
对匹配的元素集合中每个元素都注入文本内容‘val’。不能对input元素使用。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$("p").text("<b>Some</b> new text.");
});
</script>
<style>
p { color:blue; margin:8px; }
</style>
</head>
<body>
<p>Test Paragraph.</p>
</body>
</html>
$("p").text("<b>Some</b> new text.");
对匹配的p元素集合每个p元素都注入文本内容。以下为文本内容
<b>Some</b> new text.