後無法工作jQuery Sortable()運行良好,如果我嘗試銷燬並創建可排序的,也很好地工作。 但如果嘗試$(document).unbind('mousemove')
並重新創建可排序,它只能工作一次,然後永遠不會工作。 我知道我可以更改代碼。但我想知道爲什麼。
這裏是下面的代碼,還對的jsfiddle(http://jsfiddle.net/webjjin/YJEf5/)
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<div id="container">
<ul id="sortable">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</div>
<button id="btn">Destroy and create</button>
<button id="unbind">Unbind</button>
<script>$("#sortable").sortable();</script>
<script>
var html = $('#container').html();
$('#btn').click(function(){
$("#sortable").sortable('destroy');
$('#container').empty();
setTimeout(function(){
$('#container').append(html);
$("#sortable").sortable();
}, 500);
});
$('#unbind').click(function(){
jQuery(document).unbind('mousemove').unbind('mouseup');
})
</script>
可排序需要跟蹤鼠標位置,以便您可以拖動對象。與單擊和文件輸入框返回false一樣,它們停止工作。如果您有一個跟蹤鼠標的功能,您可以添加一個全局變量來啓用/禁用您的功能。即'window.notrack = true;',在你的函數中(在它的最上面):'if(window.notrack){return false; }' –
僅用於jQuery 1.7,.off()方法優於.unbind()以移除元素上的事件處理程序。 – j08691
@你爲什麼要刪除所有的事件處理程序,爲什麼你不能只是[禁用](http://api.jqueryui.com/sortable/#method-disable)排序 –