Javascript inline onclick goto local anchor

匿名 (未验证) 提交于 2019-12-03 01:47:02

问题:

I have a button as an image...

I need some javascript in an onclick where when click it will navigate to a local anchor. For example:

How can I do this?

Update: This is the code I'm using:

onclick="document.location=#goToPage';return false;"

回答1:

it looks the onClick should be:

onclick="document.location+='#goToPage';return false;" 


回答2:

The solution presented in the accepted answer has the important issue that it can only be used 1 time. Each consecutive click appends #goToPage to location and the window won't navigate to the anchor.

A solution is to remove the anchor part before appending a new anchor:

function goToAnchor(anchor) {   var loc = document.location.toString().split('#')[0];   document.location = loc + '#' + anchor;   return false; }

Usage example:

Anchor 

Note that the anchor needs to be enclosed in quotes, without the hash prefix.



回答3:

Wrap it in an anchor tag. You don't need to use JS.

 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!