Click triggered after sorting sortable jQuery UI
This problem occurs on Safari. I don't get the problem on IE (which is a first).
I have a sortable:
<ul id=开发者_开发技巧'srt'>
<li><a class='url' href='http://www.test.com'>test</a></li>
<li><a class='url' href='http://www.test1com'>test1</a></li>
<li><a class='url' href='http://www.test2com'>test2</a></li>
</ul>
and the following script:
$(document).ready( function() {
$("#srt").sortable();
$(".url").click( test );
});
function test() {
$(this).text( "done" );
return false;
}>
Clicking a link will change the text to 'done', but dragging it will also change the text to 'done'. But I don't want to click on the link at the end of a sort.
Like I said: IE is running the function like it should, it sorts the link and doesn't trigger the bound click-function, but Safari does trigger the click...
How can I prevent this from happening?
Edit: I created a jFiddle for this so you can try it out.
Actually I'm having the same problem in my current project, and i really don't have a clue since at the beginning was working properly, but after some change in code it stopped to work. the problem is that i made a lot of changes and i can't imagine which create the problem
Hwr in you case is simpler luckily. You should never pass event handler like this in jquery. Use this syntax:
$(".url").click( function(){
test();
});
This works!
EDIT: My apologies, it still trigger the handler when sorting. Two are the solutions!
The best one:
$("#srt").sortable({ helper: 'clone' });
$(".url").click( function() { $(this).text( "done" ); } );
Using helper: 'clone' option avoid the plugin to run the handlers on the elements. (note that i included the test() body into the anonymous function, otherwise you would not be able to use "this")
Uglier one and that cause problems with event bubbling:
$("#srt").sortable();
$(".url").live('click', function() { $(this).text( "done" ); } );
This is just for teaching reasons! since the live() events are not in the elements itself, they don't get triggered by sortable!
精彩评论