Home  >  Article  >  Web Front-end  >  Usage of text() in jQuery document operations

Usage of text() in jQuery document operations

一个新手
一个新手Original
2017-10-06 10:43:011513browse

Definition and usage
text() method sets or returns the text content of the selected element. It mainly includes three points:
1. Setting 2. Return 3. Use functions to set text content.

1Set text content
When this method is used to set a value, it will overwrite all the content of the selected element.
Example:
$(selector).text(content)
Parameters Content
Description Specifies the new text content of the selected element. Note: Special characters will be encoded.

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").text("Hello world!");
  });
});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button class="btn1">改变所有 p 元素的文本内容</button></body></html>

Usage of text() in jQuery document operations
Result after clicking:
Usage of text() in jQuery document operations

2. Return text content
When this method is used to return a value, it returns the combined text content of all matching elements (HTML tags removed).
$(selector).text()
For example:

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){
  $(".btn1").click(function(){
    alert($("p").text());
  });
});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button class="btn1">获得 p 元素的文本内容</button></body></html>

Usage of text() in jQuery document operations
After clicking,
Usage of text() in jQuery document operations
will return the content in the tag The value is returned.
3. Use function to set text content
Use function to set text content of all selected elements.
Syntax

$(selector).text(function(index,oldcontent))

Parameters function(index,oldcontent)
Description Required. Specifies a function that returns the new text content of the selected element.
index - optional. Accepts the index position of the selector.
html - Optional. Accepts the current contents of the selector.

For example

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){
  $("button").click(function(){
    $("p").text(function(n){
    return "这个 p 元素的 index 是:" + n;
    });
  });
});</script></head><body><p>This is a paragraph.</p><p>This is another paragraph.</p><button class="btn1">改变所有 p 元素的文本内容</button></body></html>

Usage of text() in jQuery document operations
Return the result after clicking
Usage of text() in jQuery document operations

The above is the detailed content of Usage of text() in jQuery document operations. 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