absolute
に配置された要素に対してjQuery wrap
関数を使用しようとしています。以下は私のコードです。絶対配置要素でJqueryラップ関数を使用する
子要素をクリックすると、別のdivで同じものを折り返していますが、機能していません。代わりに、ラッパー要素が画面の上部に表示されます。子要素をラップするには、css
またはjs
をどのように操作できますか。
$(document).ready(function() {
$("#child").click(function() {
$(this).wrap("<div class='wrapper'></div>")
});
});
#container {
position: relative;
height: 500px;
width: 600px;
background: #ccc
}
#child {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
background: #444;
border: 2px solid red;
height: 200px;
width: 200px;
}
.wrapper {
border: 1px dashed green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<div id="child">
</div>
</div>