我是一个jQuery新手。我搜索了文档和stackoverflow,找不到答案。我需要取消在可排序列表的最后更改时,单击一个按钮,它的工作原理。
我还需要获得元素(或它的id),它已经移动到它以前的位置。我该怎么做呢?
我的代码:
<script type="text/javascript">
$(function(){
$("#sortable").sortable({
placeholder: "ui-state-highlight",
opacity: 0.6
});
$("#cancelSort").click(function(){
$("#sortable").sortable("cancel");
});
});
</script>
<button id="cancelSort">Cancel Sort</button>
提前感谢!
创建一个变量,使用stop event
,并将变量设置为ui.item
$(function(){
var last_el=null,
last_id=null;
$("#sortable").sortable({
placeholder: "ui-state-highlight",
opacity: 0.6,
stop: function( event, ui ){
last_el = $(ui.item); //element
last_id = $(ui.item).attr('id'); //element_id
}
});
$("#cancelSort").click(function(){
$("#sortable").sortable("cancel");
console.log(last_el, last_id);
});
});
演示:http://jsfiddle.net/dirtyd77/Tmdmq/
注意:如果您不熟悉console.log
,请转到此链接。