如何用jQuery在所有段落中追加一些文字
在这篇文章中,我们将使用jQuery为所有段落元素添加一些文本。为了给所有段落元素添加一些文本,我们使用append()和document.createTextNode()方法。
jQuery append()方法用于在所选元素的末端插入一些内容。
语法:
$(selector).append( content, function(index, html) )
示例:
<!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>
输出:
在点击按钮之前:
点击按钮后:
极客教程