1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="entry-content">
<div class="entry-title roundTop">
<h1 class="jq-clearfix">.text()</h1>
<div class="entry-meta jq-clearfix">
Categories:
<span class="category"><a href="http://api.jquery.com/category/manipulation/" title="View all posts in Manipulation">Manipulation</a> > <a href="http://api.jquery.com/category/manipulation/dom-insertion-inside/" title="View all posts in DOM Insertion, Inside">DOM Insertion, Inside</a></span>
</div>
</div>
<div class="toc">
<h4><span>Contents:</span></h4>
<ul class="toc-list">
<li>
<a href="#text1">text() </a><ul><li>.text()
</li></ul>
</li>
<li>
<a href="#text2">text( textString ) </a><ul>
<li>.text( textString )
</li>
<li>.text( function(index, text) )
</li>
</ul>
</li>
</ul>
</div>
<div id="text1" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.text()</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#String">String</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Get the combined text contents of each element in the set of matched elements, including their descendants.</p>
<ul class="signatures"><li class="signature" id="text"><h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.text()</h4></li></ul>
<div class="longdesc">
<p>Unlike the <code>.html()</code> method, <code>.text()</code> can be used in both XML and HTML documents. The result of the <code>.text()</code> method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.) Consider the following HTML:</p>
<pre><div class="demo-container">
<div class="demo-box">Demonstration Box</div>
<ul>
<li>list item 1</li>
<li>list <strong>item</strong> 2</li>
</ul>
</div>
</pre>
<p>The code <code>$('div.demo-container').text()</code> would produce the following result:</p>
<p>
<code>Demonstration Box list item 1 list item 2</code>
</p>
<p>The <code>.text()</code> method cannot be used on form inputs or scripts. To set or get the text value of <code>input</code> or <code>textarea</code> elements, use the <a href="/val"><code>.val()</code></a> method. To get the value of a script element, use the <a href="/html"><code>.html()</code></a> method.</p>
<p>As of jQuery 1.4, the <code>.text()</code> method returns the value of text and CDATA nodes as well as element nodes.</p>
</div>
<h3>Example:</h3>
<div class="entry-examples" id="entry-examples"><div id="example-0">
<h4><span class="desc">Find the text in the first paragraph (stripping out the html), then set the html of the last paragraph to show it is just text (the red bold is gone).</span></h4>
<pre class="prettyprint"><code class="example demo-code"><!DOCTYPE html>
<html>
<head>
<style>
p { color:blue; margin:8px; }
b { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<p><b>Test</b> Paragraph.</p>
<p></p>
<script>
var str = $("p:first").text();
$("p:last").html(str);
</script>
</body>
</html></code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></div>
</div>
</div>
<div id="text2" class="entry method">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.text( textString )</span> <span class="returns">Returns: <a class="return" href="http://api.jquery.com/Types/#jQuery">jQuery</a></span>
</h2>
<div class="jq-box roundBottom entry-details">
<p class="desc"><strong>Description: </strong>Set the content of each element in the set of matched elements to the specified text.</p>
<ul class="signatures">
<li class="signature" id="text-textString">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.text( textString )</h4>
<p class="arguement"><strong>textString</strong>A string of text to set as the content of each matched element.</p>
</li>
<li class="signature" id="text-functionindex- text">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.text( function(index, text) )</h4>
<p class="arguement"><strong>function(index, text)</strong>A function returning the text content to set. Receives the index position of the element in the set and the old text value as arguments.</p>
</li>
</ul>
<div class="longdesc">
<p>Unlike the <code>.html()</code> method, <code>.text()</code> can be used in both XML and HTML documents. </p>
<p>We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method <code>.createTextNode()</code>, which replaces special characters with their HTML entity equivalents (such as <code>&lt;</code> for <code><</code>). Consider the following HTML:</p>
<pre><div class="demo-container">
<div class="demo-box">Demonstration Box</div>
<ul>
<li>list item 1</li>
<li>list <strong>item</strong> 2</li>
</ul>
</div>
</pre>
<p>The code <code>$('div.demo-container').text('<p>This is a test.</p>');</code> will produce the following DOM output:</p>
<pre><div class="demo-container">
&lt;p&gt;This is a test.&lt;/p&gt;
</div></pre>
<p>It will appear on a rendered page as though the tags were exposed, like this:</p>
<pre><p>This is a test</p></pre>
<p>The <code>.text()</code> method cannot be used on input elements. For input field text, use the <a href="/val">.val()</a> method.</p>
<p>As of jQuery 1.4, the <code>.text()</code> method allows us to set the text content by passing in a function.</p>
<pre>$('ul li').text(function(index) {
return 'item number ' + (index + 1);
});</pre>
<p>Given an unordered list with three <code><li></code> elements, this example will produce the following DOM output:</p>
<pre><ul>
<li>item number 1</li>
<li>item number 2</li>
<li>item number 3</li>
</ul>
</pre>
</div>
<h3>Example:</h3>
<div class="entry-examples" id="entry-examples-1"><div id="example-1-0">
<h4><span class="desc">Add text to the paragraph (notice the bold tag is escaped).</span></h4>
<pre class="prettyprint"><code class="example demo-code"><!DOCTYPE html>
<html>
<head>
<style>
p { color:blue; margin:8px; }
</style>
<script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
<p>Test Paragraph.</p>
<script>$("p").text("<b>Some</b> new text.");</script>
</body>
</html></code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div></div>
</div>
</div>
</div>
</body></html>
|