我有一个输入表单,它接受报告的参数。我将这些提交到同一页面。生成报告后,我想将用户移至报告顶部。但是,javascript 不会去 anchor 。当我的 href 是一个不同但在其他方面相同的页面时, anchor 起作用,但当它是同一页面时,它不起作用。示例:
<script type="text/javascript">
function moveto() {
window.location.href = "frag6.cfm#betty";
}
</script>
</head>
<body>
<script type = "text/javascript">
moveto()
</script>
<cfset subtest = ArrayNew(1)>
<cfloop from = "1" to = "10" index = "m">
<cfloop from = "1" to = "3" index = "i">
<cfloop from = "1" to = "4" index = "j">
<cfset test[i][j] = "#m#_#i#_#j#">
</cfloop>
</cfloop>
</cfloop>
<cfdump var = "#test#">
<cfloop from = "1" to = "3" index = "i">
<cfloop from = "1" to = "4" index = "j">
<cfset subtest[j] = test[i][j]>
</cfloop>
<cfdump var = "#subtest#">
</cfloop>
<a name = "betty"> here I am </a>
最佳答案
您应该为 anchor 使用 id 属性而不是名称,使用 location.hash 而不是 location.href
<div id="happyanchor"></div>
<script>
function goToAnchor(anchor){
location.hash = anchor;
}
goToAnchor('happyanchor');
</script>
关于javascript - 当 href 是同一页时,window.location.href 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15736763/