gpt4 book ai didi

javascript - 我想删除主 div 中拖动的插入元素

转载 作者:太空宇宙 更新时间:2023-11-04 10:16:50 25 4
gpt4 key购买 nike

这是我的代码,我想从大 div 中删除插入/删除的元素这是我的代码,请帮助我,我在代码末尾使用了 remove() 函数,但它对我不起作用。请指导我是 jquery 的新手。

$(function() {
$( "ul li" ).each(function(){
$(this).draggable({
helper: "clone"
});
});

$( ".day" ).droppable({
activeClass: "ui-state-hover",
hoverClass: "ui-state-active",
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
var targetElem = $(this).attr("id");

$( this ).addClass( "ui-state-highlight" );
if($(ui.draggable).hasClass('draggable-source'))
$( ui.draggable ).clone().appendTo( this ).removeClass('draggable-source');
else
$( ui.draggable ).appendTo( this );

console.log(this.id)
}
}).sortable({
items: "li:not(.placeholder)",
sort: function() {
$( this ).removeClass( "ui-state-default" );
}
});

});
h1 {font-family:Helvetica Neue, Helvetica, Arial, sans-serif;margin-top:0;font-size:36px;}
td {
text-align:;
display:inline-block;

}
tr:first-child td {
border-bottom:0;
border-top:0;
}
tr:nth-child(2) td {
border-top:0;
border-bottom:0;
}
td:first-child {
border-left:0;
}
td:last-child {
border-right:0;
}
div.day {
width:1000px;
min-height: 300px;
margin:0;
border: 1px solid black;

}
div#drag {
width:120px;
height:40px;
padding:5px;
margin:5px;
display:inline-block;
text-align:center;
line-height: 40px;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-weight: bold;
font-size:18px;
color:white;
text-shadow:0 1px 0 grey;
border:1px solid rgba(0,0,0,.1);
}
div#drag:hover {cursor:move;}
div#subjects ul li {
display:inline-block;
}
li, ul {
list-style-type: none;
}
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI Droppable - Default functionality</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>

</head>
<body>
<table id="days">
<tr>
<td>Drag and Drop here</td>


</tr>
<tr>
<td>
<div id="div1" class="day monday ui-widget-content">
</div>
</td>


</tr>
</table>
<ul>
<li class="draggable-source">
<div style="background-color:brown;" id="drag">Element 1</div>
</li>
<li class="draggable-source">
<div style="background-color:orange;" id="drag">Element 2</div>
</li>

</ul>
</body>

</html>

最佳答案

当元素被删除时使用 $(ui.draggable).remove():

$(function() {
$( "ul li" ).each(function(){
$(this).draggable({
helper: "clone"
});
});

$( ".day" ).droppable({
activeClass: "ui-state-hover",
hoverClass: "ui-state-active",
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
var targetElem = $(this).attr("id");

$( this ).addClass( "ui-state-highlight" );
if($(ui.draggable).hasClass('draggable-source'))
$( ui.draggable ).clone().appendTo( this ).removeClass('draggable-source');
else
$( ui.draggable ).appendTo( this );
// Remove element
$( ui.draggable ).remove();

}
}).sortable({
items: "li:not(.placeholder)",
sort: function() {
$( this ).removeClass( "ui-state-default" );
}
});

});
h1 {font-family:Helvetica Neue, Helvetica, Arial, sans-serif;margin-top:0;font-size:36px;}
td {
text-align:;
display:inline-block;

}
tr:first-child td {
border-bottom:0;
border-top:0;
}
tr:nth-child(2) td {
border-top:0;
border-bottom:0;
}
td:first-child {
border-left:0;
}
td:last-child {
border-right:0;
}
div.day {
width:1000px;
min-height: 300px;
margin:0;
border: 1px solid black;

}
div#drag {
width:120px;
height:40px;
padding:5px;
margin:5px;
display:inline-block;
text-align:center;
line-height: 40px;
font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
font-weight: bold;
font-size:18px;
color:white;
text-shadow:0 1px 0 grey;
border:1px solid rgba(0,0,0,.1);
}
div#drag:hover {cursor:move;}
div#subjects ul li {
display:inline-block;
}
li, ul {
list-style-type: none;
}
<html>
<head>
<meta charset="utf-8">
<title>jQuery UI Droppable - Default functionality</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js"></script>

</head>
<body>
<table id="days">
<tr>
<td>Drag and Drop here</td>


</tr>
<tr>
<td>
<div id="div1" class="day monday ui-widget-content">
</div>
</td>


</tr>
</table>
<ul>
<li class="draggable-source">
<div style="background-color:brown;" id="drag">Element 1</div>
</li>
<li class="draggable-source">
<div style="background-color:orange;" id="drag">Element 2</div>
</li>
<li class="draggable-source">
<input id="drag" type="button" value="dragme">
</li>
</ul>
</body>

</html>

关于javascript - 我想删除主 div 中拖动的插入元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37186024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com