jQuery draggable list with clickable objects within - preventing click on drag
I have a ul that is draggable, and there are anchors in the li's that I want to deactivate during the drag so that they're not triggered once the draggable.stop() event triggers.
<ul class="draggable-list">
<li class="list-item"><a href="#">clickable child</a></li>
<li class="list-item"><a href="#">clickable child</a></li>
...
</ul>
It's a similar situation to this: Preventing click event with jQuery drag and drop
But my draggable item is not my clickable item, my draggable item contains my clickable items.
I've tried the code from the above link, but because it's referencing the draggable object, it doesn't prevent the clicks correctly:
$("ul.draggable-list").draggable({
start: function(event, ui) {
ui.helper.bind("click.prevent",
function(event) { event.preventDefault(); });
},
stop: function(event, ui) {
setTimeout(function(){ui.helper.unbind("click.prevent");}, 300);
}
})
I tried this, to directly point to the elements I want to disable, but this has the effect of initiating a click on the first drag attempt, and then preventing ALL clicks (dragged or not) afterwards:
$("ul.draggable-list").draggable({
start: function() {
$(".list-item a").bind("click.prevent",
function(event) { event.preventDefault(); });
},
stop: function() {
setTimeout(function(){$(".list-item a").unbind("click.prevent");}, 300);
}
})
I'm 开发者_StackOverflow社区unsure how to alter the ui.helper.bind so that it's binding to the clickable children rather than the draggable parent.
Based on an example here: http://www.west-wind.com/Weblog/posts/762598.aspx
I got it working thusly:
start: function(e) {
$(".list-item a").unbind("click");
},
stop: function(e) {
setTimeout(function() {
$(".list-item a").click(function (){...});
}, 300);
}
精彩评论