コード:
HTML
<a id="go" href="javascript:void(0);">Start</a>
<div id="container">
<div class="example" id="example1">1</div>
<div class="example" id="example2">2</div>
<div class="example" id="example3">3</div>
</div>
jQuery
$('#go').click(function () {
InitDragDrop();
});
function InitDragDrop() {
var BoxImmagineFutura = null;
$('#boxTemp').remove();
BoxImmagineFutura = $('<div/>', { id: 'boxTemp', class: 'boxTempClass', text: 'Added' });
BoxImmagineFutura.appendTo($("#container"));
$('.example').mousedown(function (e) {
BoxImmagineFutura.insertBefore("#example2");
BoxImmagineFutura.show();
});
}
CSS
.example, #boxTemp
{
float:left;
width:40px;
height:40px;
margin:10px;
border:1px solid #000000;
cursor:pointer;
}
#boxTemp
{
background-color:red;
display:none;
}
順番に :
- [開始]をクリックして、関数/ハンドラーを初期化します。
- divをクリックします(マウスを押すと[追加済み]ボックスが表示されます)。
- もう一度[開始]をクリックして、すべての変数をリセットします。
- divをもう一度クリックするより:なぜ2つの追加divがあるのですか?前のものを削除しました
$('#boxTemp').remove();
この問題を解決するのを手伝ってもらえますか?削除するBoxImmagineFutura.insertBefore("#example2");
と、そのdivの「クローン」はありません。
最初の編集:
ええ、InitDragDrop()を呼び出すたびに、バインドを解除する必要がありmousedown()
ますが、これは私の問題を説明していません