通过 javascript 转到锚点
我正在尝试在 JavaScript 中创建一个函数,它将把用户带到 HTML 锚点。唯一的问题是,我正在尝试在 SharePoint 中的 .aspx 页面中创建它...
我有一个隐藏表,我使用 JavaScript 函数取消隐藏该表,但该表位于页面底部,所以我添加了桌子旁边的锚并尝试超链接到它...但它不起作用...这是我的代码:
function GoTo() {
window.location.hash="change"
}
<a name="change"></a>
有什么想法吗?
I'm trying to create a function in JavaScript which will take the user to a HTML anchor. The only thing is, I'm trying to create it in SharePoint within an .aspx page...
I have a hidden table, which I unhide with a JavaScript function, but the table is at the bottom of the page, so I added an anchor next to the table and tried to hyperlink to it... but it's not working... This is my code:
function GoTo() {
window.location.hash="change"
}
<a name="change"></a>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚尝试过,它对我有用。尝试将
#change
添加到地址栏,看看它是否符合您的预期。I just tried it, and it worked for me. Try adding
#change
to the address bar and see whether it does what you expect it to.JD-Daz -
根据您的简短规范,我创建了这个简单的测试页面,它似乎有效。也许您应该将自己的页面复制并粘贴到新页面中,然后剪切内容直到其正常工作:
JD-Daz -
Based on your brief spec, I created this simple test page, and it seems to work. Maybe you should do a copy and paste of your own page into a new page, and then cut stuff out until it works:
要解决您的问题,请将按钮更改为
问题是
input type="button"
将在单击时提交页面。因此,当您单击它时,它会向下滚动到您的锚元素,但随后会重新加载页面,从而达不到目的。您需要将return false
添加到onclick
处理程序的末尾以抑制默认操作。另外,
onclick
应该全部小写。To solve your problem, change to button to
The problem is that an
input type="button"
will submit the page when clicked. Therefore, when you click it, it scrolls down to your anchor element, but then reloads the page, defeating the purpose. You need to addreturn false
to the end of theonclick
handler to suppress the default action.Also,
onclick
should be all lower case.我认为您想使用
link text
。I think you want to use
<a href="#change" onclick="unhide table code here">link text</a>
.将
window.location.hash = "change"
更改为window.location.href = "#change"
我已经测试过了,它可以在 IE8、Chrome、FF、Safari 中运行。
可能还有另一种方法:
如果哈希方法不起作用,请尝试将窗口设置为较小的尺寸,以便它可以跳转到哈希标签
Change your
window.location.hash = "change"
towindow.location.href = "#change"
I've tested this and this is working in IE8, Chrome, FF, Safari.
There is probably another way:
If the hash method is not working try to set your window in a small size that it can jump to the hash tag