preventDefault将不能在Firefox上工作 [英] preventDefault won't work on Firefox
问题描述
我试图阻止A HREF实际打开链接,但是执行一个脚本。我的脚本部分工作,但似乎Firefox只是忽略这一点:
$(#permalink a)。 (function(id){
$(#newPost)。fadeToggle;
event.preventDefault();
var id = this.getAttribute('href');
$(#newPostContent)。load(id);
$(#newPost)。show(fast);
});
任何人都可以建议使用跨浏览器脚本来防止默认值?
< c> c>
的标准解决方案是 return false
/ code>处理程序。 return false
等效于调用 preventDefault()
和 stopPropagation()
上的事件。 preventDefault()
也足以满足这个任务,所以它或者 return false
都可以工作。
您的问题似乎是语法错误和错误的参数。我看到:
-
fadeToggle
没有括号;
- 函数参数调用
id
,但您调用 event.preventDefault()
;和
- 您要淡化该信息,然后立即显示该信息?这将排队两个动画,但没有多大的意义。您可能希望以合理的方式将这两个东西和
load()
链接在一起。
所以这应该工作:
$(#permalink a
$(#newPost)。fadeToggle();
var id = this.getAttribute('href');
$(#newPostContent)。load(id);
$(#newPost)。show(fast);
return false;
});
您可以自由替换 return false
code> event.preventDefault()
,但传播的 click()
事件可能或可能导致其他问题,事件处理程序。 I'm trying to prevent A HREF from actually opening the link, but to execute a script. I got the script part working, but it seems Firefox would just ignore this:
$("#permalink a").click(function(id){
$("#newPost").fadeToggle;
event.preventDefault();
var id = this.getAttribute('href');
$("#newPostContent").load(id);
$("#newPost").show("fast");
});
Can anyone suggest a cross-browser script for preventing defaults?
The standard solution to this is to return false
from the click()
handler. return false
is the equivalent of calling preventDefault()
and stopPropagation()
on the event. preventDefault()
is sufficient for this task too so either it or return false
will work.
Your problem seems to be syntax errors and misnamed parameters. I see:
fadeToggle
has no parentheses;- the function parameter is called
id
yet you callevent.preventDefault()
; and - You're fading the post and then immediately showing it? That'll queue two animations but doesn't make a lot of sense. You probably want to chain together those two things and the
load()
in a rational manner.
So this should work:
$("#permalink a").click(function(event) {
$("#newPost").fadeToggle();
var id = this.getAttribute('href');
$("#newPostContent").load(id);
$("#newPost").show("fast");
return false;
});
You can freely replace return false
with event.preventDefault()
in this instance but the propagating click()
event might or might cause other issues depending on what other event handlers you have.
这篇关于preventDefault将不能在Firefox上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!