如何使用 jQuery 在每个匹配元素后插入内容?
jqueryweb developmentfront end technology
要使用 jQuery 在每个匹配元素后插入内容,请使用 after() 方法。
after(content) 方法会在每个匹配元素后插入内容。
以下是此方法使用的所有参数的说明。 −- content− 在每个目标元素后插入内容。可以是 HTML 或文本内容。
示例
您可以尝试运行以下代码,了解如何在每个匹配的元素后插入内容:
<html>
<head>
<title>jQuery after() method</title>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$(this).after('<div class = "div"></div>' );
});
});
</script>
<style>
.div {
margin:10px;
padding:12px;
border:2px solid #666;
width:60px;
}
</style>
</head>
<body>
<p>Click on any square below to see the result:</p>
<div class = "div" style = "background-color:blue;"></div>
<div class = "div" style = "background-color:green;"></div>
<div class = "div" style = "background-color:red;"></div>
</body>
</html>

