我希望在可排序容器中使用click事件交换两个元素,但我不能这样做。
http://jsfiddle.net/prince4prodigy/rvfYE/
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
$('#click').bind('click', function(){
$('#li-3').animate({index:0},500);
$('#li-1').animate({index:2},500);
});
答案 0 :(得分:1)
animate
方法无法识别 index 属性,此方法主要用于处理数字CSS属性和其他一些特定属性,但是您可以更改其位置使用prepend
和insertAfter
方法的元素:
var $sortable = $("#sortable").sortable();
$sortable.disableSelection();
$('#click').on('click', function () {
$('#li-1').insertAfter($sortable.children().eq(1));
$('#li-3').prependTo($sortable);
});
使用slideDown
和slideUp
方法的替代方法:
$('#click').bind('click', function () {
$('#li-1').slideUp(400, function () {
$(this).insertAfter($sortable.children().eq(2)).slideDown(400);
});
$('#li-3').slideUp(400, function () {
$(this).prependTo($sortable).slideDown(400);
});
});
答案 1 :(得分:0)
正如我在上面的评论中所说:
$(function(){
$("li").click(function(){
//have we clicked on the same item ?
if($(this).hasClass("clicked")){
$(this).removeClass("clicked");
}
// is there an element that allready have been clicked
else{
var firstClicked = $(".clicked");
//if no, this the first clicked element
if(typeof firstClicked == undefined){
$(this).addClass("clicked");
}
else{
swap()....//do your stuff to swapp the elements
}
});
});