我成功设计了一个带有mousedown和mousemove的拖放。这是代码:
$('.item').live( {
mousedown : function() {
var $elem = $(this);
if (self.selectedItem != null) {
self.selectedItem.removeClass('selected');
self.selectedItem = null;
firstSelectedItem = null;
switchingItem = false;
} else {
self.selectedItem = $elem.addClass('selected');
firstSelectedItem = $elem;
switchingItem = true;
}
},
mousemove : function() {
var $elem = $(this);
if (switchingItem === true && $elem.attr('id') != firstSelectedItem.attr('id')) {
self._swap(firstSelectedItem, $elem);
self.selectedItem.removeClass('selected');
self.selectedItem = null;
firstSelectedItem = null;
switchingItem = false;
}
},
});
我试图用touchevents再次做到这一点。除了$ this这个值与touchstart和touchmove相同,所以交换不起作用:
$('.item').live( {
touchstart : function(e) {
e.stopPropagation();
e.preventDefault();
var $elem = $(this);
if (self.selectedItem != null) {
self.selectedItem.removeClass('selected');
self.selectedItem = null;
firstSelectedItem = null;
switchingItem = false;
} else {
self.selectedItem = $elem.addClass('selected');
firstSelectedItem = $elem;
switchingItem = true;
console.log (firstSelectedItem.attr('id'));
}
},
touchmove : function (e) {
e.stopPropagation();
e.preventDefault();
var elem = $(this);
if (switchingItem === true && $elem.attr('id') != firstSelectedItem.attr('id')) {
self._swap(firstSelectedItem, $elem);
self.selectedItem.removeClass('selected');
self.selectedItem = null;
firstSelectedItem = null;
switchingItem = false;
}
},
});
怎么办?
非常感谢