带有 Javascript onclick 事件的 HTML 锚标记 [英] HTML anchor tag with Javascript onclick event

查看:18
本文介绍了带有 Javascript onclick 事件的 HTML 锚标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Google 时,我发现他们在锚标记中使用 onclick 事件.

On using Google I found that they are using onclick events in anchor tags.

在 google header 部分的 more 选项中,它看起来像一个普通的标签,但点击它不会被重定向,而是打开了一个菜单.通常在使用时

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(),但我在该页面本身显示了一个菜单.google 怎么做?

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>

不管怎样,谷歌是否这样做并不重要.将您的 onclick 函数绑定到 javascript 中会更简洁 - 这样您就可以将 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆