如何使用jQuery在元素之前附加元素?
要将元素附加在元素之前,请使用insertBefore()方法。insertBefore(选择器)方法将所有匹配的元素插入到另一个指定的元素集之前。
示例
<html>
<head>
<title>The jQuery Example</title>
<script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("div").click(function () {
$("#source").insertBefore(this);
});
});
</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>
<div class = "div" id = "source"></div>
</body>
</html>热门推荐
5 甄嬛传祝福语简短
10 上级买车祝福语大全简短
11 生日哥哥祝福语简短独特
12 年后祝福语简短走心
13 韩语贺卡祝福语简短版
14 对上司的简短祝福语
15 初八的祝福语简短
16 宝宝新年祝福语大全简短
17 女侠老师祝福语简短
18 祝产妇的祝福语简短