jquery 手册
Ajax
属性(Attributes)
回调对象(Callbacks ..
核心(Core)
CSS
数据(Data)
延迟对象(Deferred o ..
尺寸(Dimensions)
效果(Effects)
事件(Events)
表单(Forms)
文档操作(Manipulati ..
杂项(Miscellaneou ..
位置(Offset)
插件编写(Plugin Aut ..
属性(Properties)
选择器(Selectors)
遍历(Traversing)
工具(Utilities)

Returns: JQuery(":text")

Description: Selects all elements of type text.
  • version added: 1.0
  • JQuery(":text")

$(':text') is equivalent to $('[type=text]') and thus selects all <input type="text"> elements. As with other pseudo-class selectors (those that begin with a ":") it is recommended to precede it with a tag name or some other selector; otherwise, the universal selector ("*") is implied. In other words, the bare $(':text') is equivalent to $('*:text'), so $('input:text') should be used instead.

Note: As of jQuery 1.5.2, :text selects input elements that have no specified type attribute (in which case type="text" is implied).

Examples:

Example:

Finds all text inputs.
<!DOCTYPE html>
<html>
<head>
  <script src="../jquery.min.js"></script>
  <style>textarea { height:25px; }</style>
</head>
<body>
  <form>
    <input type="button" value="Input Button"/>
    <input type="checkbox" />

    <input type="file" />
    <input type="hidden" />
    <input type="image" />

    <input type="password" />
    <input type="radio" />
    <input type="reset" />

    <input type="submit" />
    <input type="text" />
    <select><option>Option</option></select>

    <textarea></textarea>
    <button>Button</button>
  </form>
  <div>
  </div>
  <script>var input = $("form input:text").css({background:"yellow", border:"3px red solid"});
    $("div").text("For this type jQuery found " + input.length + ".")
            .css("color", "red");
    $("form").submit(function () { return false; }); // so it won't submit</script>
</body>
</html>

Demo:

Returns: StringString .text( )

Description: Get the combined text contents of each element in the set of matched elements, including their descendants.
  • version added: 1.0
  • .text( )

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() 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:

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

The code $('div.demo-container').text() would produce the following result:

Demonstration Box list item 1 list item 2

The .text() method cannot be used on form inputs or scripts. To set or get the text value of input or textarea elements, use the .val() method. To get the value of a script element, use the .html() method.

As of jQuery 1.4, the .text() method returns the value of text and CDATA nodes as well as element nodes.

Examples:

Example:

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).
<!DOCTYPE html>
<html>
<head>
  <script src="../jquery.min.js"></script>
  <style>p { color:blue; margin:8px; }
  b { color:red; }</style>
</head>
<body>
  <p><b>Test</b> Paragraph.</p>

  <p></p>
  <script>var str = $("p:first").text();
    $("p:last").html(str);</script>
</body>
</html>

Demo:

Returns: jQueryjQuery .text( Function function(index, text) )

Description: Set the content of each element in the set of matched elements to the specified text.
  • version added: 1.0
  • .text( String textString )
  • textString A string of text to set as the content of each matched element.
  • version added: 1.4
  • .text( Function function(index, text) )
  • function(index, text) 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.

Unlike the .html() method, .text() can be used in both XML and HTML documents.

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 .createTextNode(), which replaces special characters with their HTML entity equivalents (such as &lt; for <). Consider the following HTML:

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

The code $('div.demo-container').text('<p>This is a test.</p>'); will produce the following DOM output:

<div class="demo-container">
&lt;p&gt;This is a test.&lt;/p&gt;
</div>

It will appear on a rendered page as though the tags were exposed, like this:

<p>This is a test</p>

The .text() method cannot be used on input elements. For input field text, use the .val() method.

As of jQuery 1.4, the .text() method allows us to set the text content by passing in a function.

$('ul li').text(function(index) {
  return 'item number ' + (index + 1);
});

Given an unordered list with three <li> elements, this example will produce the following DOM output:

<ul>
  <li>item number 1</li>
  <li>item number 2</li>
  <li>item number 3</li>
</ul>

Examples:

Example:

Add text to the paragraph (notice the bold tag is escaped).
<!DOCTYPE html>
<html>
<head>
  <script src="../jquery.min.js"></script>
  <style>p { color:blue; margin:8px; }</style>
</head>
<body>
  <p>Test Paragraph.</p>
  <script>$("p").text("<b>Some</b> new text.");</script>
</body>
</html>

Demo: