带有Javascript onclick事件的HTML锚标记 [英] HTML anchor tag with Javascript onclick event
问题描述
在使用Google时,我发现他们在锚标签中使用onclick事件。
On using Google I found that they are using onclick events in anchor tags.
在google标题部分的更多选项中,它看起来像像普通的标签,但点击它不会被重定向,但打开一个菜单。通常情况下使用
In more option in google header part, it looks like normal a tag, but onclicking it doesn't get redirected but opened a menu. Normally when using
<a href='more.php' onclick='show_more_menu()'>More >>></a>
通常会在没有解雇的情况下转到'more.php' show_more_menu()
,但我在该页面中显示了一个菜单。怎么办谷歌?
It usually goes to 'more.php' without firing show_more_menu()
, but I have show a menu in that page itself. How to do like google?
推荐答案
如果您的onclick函数返回false,则取消默认浏览器行为。因此:
If your onclick function returns false the default browser behaviour is cancelled. As such:
<a href='http://www.google.com' onclick='return check()'>check</a>
<script type='text/javascript'>
function check()
{
return false;
}
</script>
无论哪种方式,谷歌是否做到这一点并不重要。在javascript中绑定你的onclick函数更简洁 - 这样你就可以将HTML与其他代码分开了。
Either way, whether google does it or not isn't of much importance. It's cleaner to bind your onclick functions within javascript - this way you separate your HTML from other code.
这篇关于带有Javascript onclick事件的HTML锚标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!