如何使用jQuery在div之前附加div?
问题描述:
如何在指定的div ID之前附加div或html元素?
How can I append the div or html element before the specified div id?
<== Here I want to insert the New div tag (<div id='tt'>test</div>)
Div1
Div1 End
我尝试放置wrap(),prepend(),但都没有使用.
I tried to placed the wrap(), prepend() but both are not used.
答
您可以使用 before() .
You can use before().
在匹配的元素集中的每个元素之前插入由参数指定的内容
Insert content, specified by the parameter, before each element in the set of matched elements
$('#div1').before('<div id="tt">test</div>');
#div1 {
width: 100px;
height: 100px;
background: red;
}
#tt {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="div1">Div 1</div>