当href是同一页面时,window.location.href无效

时间:2013-04-01 02:11:27

标签: javascript cfml

我有一个输入表单,它接受报告的参数。我将这些提交到同一页面。生成报告时,我想将用户移到报告的顶部。但是,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>

1 个答案:

答案 0 :(得分:1)

您应该使用 id 属性代替名称而使用 location.hash 而不是location.href

<div id="happyanchor"></div>

<script>
function goToAnchor(anchor){
    location.hash = anchor;
}

goToAnchor('happyanchor');
</script>