【发布时间】:2014-08-07 13:34:43
【问题描述】:
在执行 javascript 函数后网页返回到页面顶部有一个小问题。
基本上,我有一个小的 javascript 函数,它通过更改 div 的显示样式来切换 div 的可见性。代码如下:
<script type="text/javascript">
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'block')
e.style.display = 'none';
else
e.style.display = 'block';
}
然后我使用如下所示的链接调用它:
<a href="#" onclick="toggle_visibility('001');" class="expand">[+/-] Hide/Show Info</a>
div 看起来像这样:
<div id="001" style="display:none;">
hello world
</div>
这很好用。但是当我点击我的“展开/隐藏”链接来切换 div 的可见性时,页面总是返回到顶部,所以我每次都必须向下滚动到底部。
我尝试在我的 javascript 函数末尾添加以下更改,但它们都不起作用:
window.location = 'test.php#' + id; //where test.php is my current page
和
window.location.hash=id;
如果能解决此问题,我们将不胜感激 谢谢。
【问题讨论】:
-
不推荐在脚本标签中使用
<!-- -->。 -
@reporter 谢谢,我会删除它
标签: javascript html css