问题描述
|
我正在尝试通过跳到锚点来打开div。开头部分正在工作,但没有跳到指定的锚点
这是我的脚本:
<script type=\"text/javascript\">
function spoil(id){
if (document.getElementById) {
var divid = document.getElementById(id);
divid.style.display = (divid.style.display = \'block\');
window.location = \'#\' + id;
}
}
</script>
<a href=\"http://example.com\" onclick=\"spoil(\'thanks\');\" title=\"hello\">
<img src=\"images/gfx.png\" alt=\"world\" width=\"300\" height=\"300\"/>
</a>
任何想法有什么问题吗?
干杯。
解决方法
看起来您正在取消隐藏剧透div。如果是这样,则可以按如下所示将元素滚动到视图中:
function spoil(id) {
var divid = document.getElementById(id);
divid.style.display = \'block\';
divid.scrollIntoView(true);
return false;
}
...
<a href=\"#\" onclick=\"return spoil(\'thanks\');\" title=\"hello\"><img src=\"images/gfx.png\" alt=\"world\" width=\"300\" height=\"300\"/></a>
,您尝试过window.location.hash = \'#\'+id;
吗?