使用 jquery 添加和删除点击时的 div
我有一个空的 div,其中有另一个可拖动的 div。
现在,无论我在哪里单击容器,可拖动 div 都应附加到 (0,0) 位置,当单击关闭按钮时,我需要删除该可拖动 div。我该怎么做?
这就是我所拥有的:
这是我的代码:
<div id="content" style="background-color: #E5E5E5; width:500px; height:500px;">
<div class="demo" style="border-style: dashed; background-color: #CCCCCC">
脚本:
<script type="text/javascript">
$(function () {
$('.demo').draggable({
containment: '#content',
cursor: 'move',
snap: '#content',
stop: function () {
var offset = $(this).offset();
var xPos = offset.left;
var yPos = offset.top;
$('#posX').text('X: ' + xPos);
$('#posY').text('Y: ' + yPos);
}
})
.resizable();
});
</script>
CSS:
.demo {
position: relative;
height: 200px;
width: 200px;
border: 1px solid #000;
}
.demo:before {
content: '';
position: absolute;
height: 30px;
width: 30px;
top: -16px;
left: -16px;
background: url(http://dummyimage.com/30x30/000/fff&text=close);
border: 1px solid #000;
}
.container
{
background-color: #E5E5E5;
width:500px;
height:500px;
}
I have an empty div and in that I have another div which is draggable.
Now wherever I click on the container the draggable div should be appended to (0,0) position and when clicked close button I need to remove that draggable div.How do I do that?
This is what I have:
Here is my code:
<div id="content" style="background-color: #E5E5E5; width:500px; height:500px;">
<div class="demo" style="border-style: dashed; background-color: #CCCCCC">
Script:
<script type="text/javascript">
$(function () {
$('.demo').draggable({
containment: '#content',
cursor: 'move',
snap: '#content',
stop: function () {
var offset = $(this).offset();
var xPos = offset.left;
var yPos = offset.top;
$('#posX').text('X: ' + xPos);
$('#posY').text('Y: ' + yPos);
}
})
.resizable();
});
</script>
CSS:
.demo {
position: relative;
height: 200px;
width: 200px;
border: 1px solid #000;
}
.demo:before {
content: '';
position: absolute;
height: 30px;
width: 30px;
top: -16px;
left: -16px;
background: url(http://dummyimage.com/30x30/000/fff&text=close);
border: 1px solid #000;
}
.container
{
background-color: #E5E5E5;
width:500px;
height:500px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 HTML 更改为:
然后将 CSS 中的
.demo:before
更改为.closebtn
并将其添加到 JavaScript 中:http://jsfiddle.net/mblase75/g6cdL/48/
Change your HTML to:
Then change
.demo:before
in your CSS to.closebtn
and add this to your JavaScript:http://jsfiddle.net/mblase75/g6cdL/48/