Home > Web Front-end > JS Tutorial > body text

Usage of text() in jQuery document operations

一个新手
Release: 2017-10-06 10:43:01
Original
1550 people have browsed it

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>
Copy after login

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>
Copy after login

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))
Copy after login

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!