[英]Firefox link to javascript function opens a new window when not intended
I have this problem where when I have this html in firefox it opens a new window 我有这个问题,当我在Firefox中有这个html它打开一个新窗口
<a style="float:right;"
href='javascript:window.location.href="#";'onClick="javascript:addNewRecord();">
New Record</a>
I have tried self.location, window.location, #body, and #h1 as the href. 我尝试过self.location,window.location,#body和#h1作为href。
Originally I had the code as, but in firefox that did not do anything but open a fresh window, and not perform my function. 最初我有代码,但在firefox中没有做任何事情,只是打开一个新窗口,而不是执行我的功能。 The code works perfect in chrome.
该代码在chrome中非常完美。
<a style="float:right;" href="javascript:addNewRecord();">New Record</a>
The canonical inline way is 规范的内联方式是
<a style="float:right;" href="#"
onClick="addNewRecord(); return false">New Record</a>
or better: 或更好:
<a style="float:right;" href="#"
onClick="return addNewRecord()">New Record</a>
where addNewRecord returns false at the end of the function 其中addNewRecord在函数末尾返回false
An even better way is 更好的方法是
window.onload=function() {
document.getElementById("addLink").onclick=addNewRecord;
}
function addNewRecord() {
...
return false;
}
plus 加
<style>
#addLink { float:right }
</style>
and 和
<a href="#" id="addLink">New Record</a>
Since abusing the HREF on a link going nowhere just to get a pointer is frowned upon, you may consider a <span>
with an onclick and a cursor:pointer. 由于在链接上滥用HREF只是为了得到一个指针是不受欢迎的,你可以考虑带有onclick和cursor:指针的
<span>
。 It does need more effort to make such an element accessible to for example screen readers. 它需要更多努力才能使例如屏幕阅读器可以访问这样的元素。
尝试:
onClick="addNewRecord();return false"
How your code behaves depends entirely on what the addNewRecord() function does (including what it returns). 代码的行为方式完全取决于addNewRecord()函数的作用(包括它返回的内容)。
Without seeing inside that function it's hard to tell, but I'd say that what is happening is inside there. 没有看到内部的功能很难说,但我会说发生的事情就在那里。
Note that what you put in the href="" part probably is not affecting the behaviour you're seeing. 请注意,您在href =“”部分中放置的内容可能不会影响您所看到的行为。
尝试这个
<a onclick="javascript:addNewRecord();">New Record</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.