如何刷新页面后定位到锚?

applerockhool 2011-01-20 04:09:36
如题, 怎么样用js先刷新页面,然后定位到锚


window.location = window.location.pathname + '#comment';
window.location.reload();


现在这样做的,不过只有firefox下好使,其他都不灵呢,js高手来帮忙
...全文
452 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
beowulf2005 2011-01-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 tcwsyt 的回复:]

楼上的是种办法。。不过多次以后就会变成xxx.html#comment#comment#comment#comment....这样了。。
[/Quote]

那简单阿,调用之前先判断下location含不含comment就行了。
含的话,先去掉,再加上!
hackerster0324 2011-01-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hackerster0324 的回复:]

JScript code
<script type="text/javascript">
function goAnchor(anchorStr){
var hash=window.location.hash;
var href=window.location.href;
return href.substr(0,href.length-hash.length);
……
[/Quote]
goAnchor('#abc')
hackerster0324 2011-01-21
  • 打赏
  • 举报
回复
<script type="text/javascript">
function goAnchor(anchorStr){
var hash=window.location.hash;
var href=window.location.href;
return href.substr(0,href.length-hash.length);
window.location.href=hash==""?(href+anchorStr):(href.substr(0,href.length-hash.length)+anchorStr);
}
</script>
灬上海爽爷 2011-01-21
  • 打赏
  • 举报
回复
楼上的是种办法。。不过多次以后就会变成xxx.html#comment#comment#comment#comment....这样了。。
linshenxiu 2011-01-21
  • 打赏
  • 举报
回复
直接用window.location=window.location.href + "#comment";就可以了。
applerockhool 2011-01-20
  • 打赏
  • 举报
回复
csdn落寞了,技术问题都没人解决了?

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧