排序后触发点击可排序 jQuery UI
此问题发生在 Safari 上。我在 IE 上没有遇到这个问题(这是第一次)。
我有一个可排序的:
<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>
和以下脚本:
$(document).ready( function() {
$("#srt").sortable();
$(".url").click( test );
});
function test() {
$(this).text( "done" );
return false;
}>
单击链接会将文本更改为“完成”,但拖动它也会将文本更改为“完成”。但我不想点击排序末尾的链接。
就像我说的:IE 正在按应有的方式运行该功能,它对链接进行排序并且不会触发绑定的单击功能,但 Safari 确实会触发单击...
我怎样才能防止这种情况发生?
编辑:我为此创建了一个 jFiddle ,以便您可以尝试一下。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,我在当前的项目中遇到了同样的问题,而且我真的没有任何线索,因为一开始工作正常,但在代码发生一些变化后,它停止工作。问题是我做了很多改变,我无法想象哪个会造成问题,
幸运的是,在你的情况下,Hwr 更简单。你永远不应该在 jquery 中传递这样的事件处理程序。使用这个语法:
这有效!
编辑:
抱歉,排序时它仍然会触发处理程序。解决办法有两个!
最好的一个:
使用 helper: 'clone' 选项避免插件在元素上运行处理程序。 (请注意,我将 test() 主体包含到匿名函数中,否则您将无法使用“this”)
更丑陋的一个会导致事件冒泡问题:
这只是出于教学原因!由于 live() 事件不在元素本身中,因此它们不会被 sortable 触发!
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:
This works!
EDIT:
My apologies, it still trigger the handler when sorting. Two are the solutions!
The best one:
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:
This is just for teaching reasons! since the live() events are not in the elements itself, they don't get triggered by sortable!