如何用jQuery在所有段落中追加一些文字

如何用jQuery在所有段落中追加一些文字

在这篇文章中,我们将使用jQuery为所有段落元素添加一些文本。为了给所有段落元素添加一些文本,我们使用append()和document.createTextNode()方法。

jQuery append()方法用于在所选元素的末端插入一些内容。

语法:

$(selector).append( content, function(index, html) )
JavaScript

示例:

<!DOCTYPE html>
<html lang="en">
  
<head>
  
    <!-- Import jQuery cdn library -->
    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script>
  
    <script>
        (document).ready(function () {
            ("button").click(function () {
                $("p").append(document
                .createTextNode(" GeeksforGeeks"));
            });
        });
    </script>
</head>
  
<body style="text-align: center;">
    <h1 style="color: green;">
        GeeksforGeeks
    </h1>
  
    <h3>
        How to append some text to
        all paragraphs using jQuery?
    </h3>
  
    <p>
        Computer Science Portal
    </p>
  
    <p>Welcome</p>
  
    <button>Click Here!</button>
</body>
  
</html>
HTML

输出:

在点击按钮之前:

如何用jQuery在所有段落中追加一些文字?

点击按钮后:

如何用jQuery在所有段落中追加一些文字?

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册