你應該分派事件:
DEMO jsFiddle
$(".draggable").draggable();
$(".draggable input").on('mousedown', function (e) {
var mdown = document.createEvent("MouseEvents");
mdown.initMouseEvent("mousedown", false, true, window, 0, e.screenX, e.screenY, e.clientX, e.clientY, true, false, false, true, 0, null);
$(this).closest('.draggable')[0].dispatchEvent(mdown);
});
對於更期望的行爲,請參閱:
DEMO jsFiddle (complete)
$(".draggable").draggable({
start: function(event, ui) {
$(this).data('preventBehaviour', true);
}
});
$(".draggable input").on('mousedown', function (e) {
var mdown = document.createEvent("MouseEvents");
mdown.initMouseEvent("mousedown", false, true, window, 0, e.screenX, e.screenY, e.clientX, e.clientY, true, false, false, true, 0, null);
$(this).closest('.draggable')[0].dispatchEvent(mdown);
}).on('click', function(e){
var $draggable = $(this).closest('.draggable');
if($draggable.data("preventBehaviour")){
e.preventDefault();
$draggable.data("preventBehaviour", false)
}
});
編輯如下評論有關jQuery UI的排序:
讓分派的事件泡到排序的容器:
SORTABLE DEMO
mdown.initMouseEvent("mousedown", true, true, window, 0, e.screenX, e.screenY, e.clientX, e.clientY, true, false, false, true, 0, null);
$(this).closest('.item')[0].dispatchEvent(mdown);
你的第二小提琴就是我要找的。謝謝!! – lhan
如果您使用'sortable'而不是'draggable',該怎麼辦?我無法以同樣的方式得到它。你有什麼機會看看這個小提琴? http://jsfiddle.net/55bxX/ – lhan
你需要讓調度的事件泡到容器的水平(第二個參數設置爲true):'mdown.initMouseEvent( 「鼠標按下」,真正的,真實的,窗口,0,e.screenX ,e.screenY,e.clientX,e.clientY,真的,假的,假的,真實的,0,NULL);'http://jsfiddle.net/55bxX/1/ –