HTML锚链接 - href和onclick两者? [英] HTML anchor link - href and onclick both?

查看:100
本文介绍了HTML锚链接 - href和onclick两者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创作一个执行一些JavaScript的锚标签,然后继续到 href 所采取的任何地方。调用执行JavaScript的函数,然后将 window.location top.location 设置为 href 位置对我不起作用。

所以,假设我在页面上有一个ID为Foo的元素。我想创作一个类似于以下内容的锚:

 < a href =#Fooonclick =runMyFunction(); return假;>做到了!< / a> 

单击此项时,我想执行runMyFunction,然后将页面跳转到 #Foo (不会导致重新加载 - 使用 top.location 会导致它重新加载页面。)



建议?我很高兴使用jQuery,如果它可以帮助这里...

解决方案

只需返回true 代替
$ b onClick
代码的返回值决定了链接固有的点击操作是否被处理 - 返回 false 表示它没有被处理,但是如果你返回 true ,那么浏览器将在函数返回并转到正确的锚点后继续处理它。


I want to author an anchor tag that executes some JavaScript and then proceeds to go wherever the href was taking it. Invoking a function that executes my JavaScript and then sets window.location or top.location to the href location doesn't work for me.

So, imagine I have an element with id "Foo" on the page. I want to author an anchor similar to:

<a href="#Foo" onclick="runMyFunction(); return false;">Do it!</a>

When this is clicked, I want to execute runMyFunction and then jump the page to #Foo (not cause a reload - using top.location would cause it to reload the page).

Suggestions? I am happy to use jQuery if it can help here...

解决方案

Just return true instead?

The return value from the onClick code is what determines whether the link's inherent clicked action is processed or not - returning false means that it isn't processed, but if you return true then the browser will proceed to process it after your function returns and go to the proper anchor.

这篇关于HTML锚链接 - href和onclick两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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