如何将前一个元素添加到当前 jQuery 选择中?
jqueryweb developmentfront end technology
要将前一个元素添加到当前 jQuery 选择中,请使用 insertBefore() 方法。
示例
您可以尝试运行以下代码来了解如何使用 insertBefore() 方法:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("<span>Demo text</span>").insertBefore("p");
});
});
</script>
</head>
<body>
<button>Insert</button>
<br>
<p>This is a paragraph.</p>
</body>
</html>

