Javascript 访问事件以从源自标记的 onclick 属性的自定义函数调用 preventdefault
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8614438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access event to call preventdefault from custom function originating from onclick attribute of tag
提问by Omu
I have links like this:
我有这样的链接:
<a href="#" onclick="myfunc({a:1, b:'hi'})" />click</a>
<a href="#" onclick="myfunc({a:3, b:'jo'})" />click</a>
And I would like to do a preventDefault()
inside myfunc()
, because a #
will be added in the address bar when clicking on the link
(without doing return false;
or href='javascript:void(0);'
)
我想做一个preventDefault()
inside myfunc()
,因为#
点击链接时地址栏中会添加一个(不做return false;
or href='javascript:void(0);'
)
Is this possible?
Can I get the event inside myfunc()
这可能吗?我可以得到里面的事件吗myfunc()
回答by Russ Cam
I believe you can pass in event
into the function inline which will be the event
object for the raised event in W3C compliant browsers (i.e. older versions of IE will still require detection inside of your event handler function to look at window.event
).
我相信您可以传入event
内联函数,该函数将成为event
W3C 兼容浏览器中引发事件的对象(即旧版本的 IE 仍需要在您的事件处理程序函数内部进行检测才能查看window.event
)。
function sayHi(e) {
e.preventDefault();
alert("hi");
}
<a href="http://google.co.uk" onclick="sayHi(event);">Click to say Hi</a>
- Run it as is and notice that the link does no redirect to Google after the alert.
- Then, change the
event
passed into theonclick
handler to something else likee
, click run, then notice that the redirection does take place after the alert (the result pane goes white, demonstrating a redirect).
- 按原样运行它并注意该链接在警报后不会重定向到 Google。
- 然后,将
event
传入onclick
处理程序的内容更改为其他类似的内容e
,单击运行,然后注意重定向确实发生在警报之后(结果窗格变为白色,表明重定向)。
回答by JuLo
The simplest solution simply is:
最简单的解决方案就是:
<a href="#" onclick="event.preventDefault(); myfunc({a:1, b:'hi'});" />click</a>
It's actually a good way of doing cache busting for documents with a fallback for no JS enabled browsers (no cache busting if no JS)
这实际上是一种对文档进行缓存破坏的好方法,并为没有启用 JS 的浏览器提供回退(如果没有 JS,则没有缓存破坏)
<a onclick="
if(event.preventDefault) event.preventDefault(); else event.returnValue = false;
window.location = 'http://www.domain.com/docs/thingy.pdf?cachebuster=' +
Math.round(new Date().getTime() / 1000);"
href="http://www.domain.com/docs/thingy.pdf">
If JavaScript is enabled, it opens the PDF with a cache busting query string, if not it just opens the PDF.
如果启用了 JavaScript,它会使用缓存破坏查询字符串打开 PDF,否则它只会打开 PDF。
回答by Suave Nti
Try this:
尝试这个:
<script>
$("a").click(function(event) {
event.preventDefault();
});
</script>
回答by Aram Mkrtchyan
<script type="text/javascript">
$('a').click(function(){
return false;
});
<script>
回答by aaandre
Add a unique class to the links and a javascript that prevents default on links with this class:
向链接添加一个独特的类和一个防止默认链接到此类的 javascript:
<a href="#" class="prevent-default"
onclick="$('.comment .hidden').toggle();">Show comments</a>
<script>
$(document).ready(function(){
$("a.prevent-default").click(function(event) {
event.preventDefault();
});
});
</script>
回答by Ronan
I think when we use onClick we want to do something different than default. So, for all your links with onClick:
我认为当我们使用 onClick 时,我们想做一些不同于默认的事情。因此,对于所有与 onClick 的链接:
$("a[onClick]").on("click", function(e) {
return e.preventDefault();
});
回答by Leigh Ciechanowski
Can you not just remove the href attribute from the a tag?
你不能从 a 标签中删除 href 属性吗?
回答by Jake
Simple!
简单的!
onclick="blabla(); return false"
回答by gdarcan
You can access the event from onclick like this:
您可以像这样从 onclick 访问事件:
<button onclick="yourFunc(event);">go</button>
and at your javascript function, my advice is adding that first line statement as:
在您的 javascript 函数中,我的建议是将第一行语句添加为:
function yourFunc(e) {
e = e ? e : event;
}
then use everywhere e as event variable
然后到处使用 e 作为事件变量
回答by rahul singh Chauhan
e.preventDefault(); from https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault
e.preventDefault(); 来自https://developer.mozilla.org/en-US/docs/Web/API/event.preventDefault
Or have return false from your method.
或者从您的方法返回 false。