CodeLab
Show Output
 
xxxxxxxxxx
32
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>jQuery Get Text Contents of the Elements</title>
6
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
7
<script>
8
$(document).ready(function(){
9
    $(".btn-one").click(function(){
10
        var str = $("p").text();
11
        alert(str);
12
    });
13
    $(".btn-two").click(function(){
14
       var str = $("p:first").text();
15
       alert(str);
16
    });
17
    $(".btn-three").click(function(){
18
       var str = $("p.extra").text();
19
       alert(str);
20
    });
21
});
22
</script>
23
</head>
24
<body>
25
    <button type="button" class="btn-one">Get All Paragraph's Text</button>
26
    <button type="button" class="btn-two">Get First Paragraph's Text</button>
27
    <button type="button" class="btn-three">Get Last Paragraph's Text</button>
28
    <p>This is a paragraph.</p>
29
    <p>This is another paragraph.</p>
30
    <p class="extra">This is one more paragraph.</p>
31
</body>
32
</html>
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。