HTML锚链接 - href和onclick两者? [英] HTML anchor link - href and onclick both?
问题描述
我想创作一个执行一些JavaScript的锚标签,然后继续到 href
所采取的任何地方。调用执行JavaScript的函数,然后将 window.location
或 top.location
设置为 href
位置对我不起作用。
< a href =#Fooonclick =runMyFunction(); return假;>做到了!< / a>
单击此项时,我想执行runMyFunction,然后将页面跳转到 #Foo
(不会导致重新加载 - 使用 top.location
会导致它重新加载页面。)
建议?我很高兴使用jQuery,如果它可以帮助这里...
只需返回true
代替
代码的返回值决定了链接固有的点击操作是否被处理 - 返回
$ b 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屋!