当href是同一页时,Window.location.href不工作

window.location.href not working when href is same page

本文关键字:href Window location 工作 一页      更新时间:2023-09-26

我有一个输入表单,它接受报告的参数。我把这些提交到同一个页面。当生成报表时,我希望将用户移动到报表的顶部。然而,javascript不会去锚。当我的href是一个不同的,但在其他方面相同的页面锚工作,但当它是相同的页面它不。例子:

<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>

您应该使用id属性来代替名称和位置。hash代替location.href

<div id="happyanchor"></div>
<script>
function goToAnchor(anchor){
    location.hash = anchor;
}
goToAnchor('happyanchor');
</script>