如何使用ASP.NET实现页面导航至特定代码锚点?

2026-04-30 11:2910阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计275个文字,预计阅读时间需要2分钟。

如何使用ASP.NET实现页面导航至特定代码锚点?

我有一个简单的Web窗体,代码如下:

Web窗体

POST后我想导航用戶到我的主播消消页面。

我有一个简单的Web窗体,代码如下:

如何使用ASP.NET实现页面导航至特定代码锚点?

//... //tons of text //... <a name="message" /> //... //tons of text //... <asp:Button ID="ButtonSend" runat="server" text="Send" onclick="ButtonSend_Click" />

POST后我想导航用户到我的主播“消息”.我有以下代码:

protected void ButtonSend_Click(object sender, EventArgs e) { this.ClientScript.RegisterStartupScript(this.GetType(), "navigate", "window.location.hash='#message';", true); }

这个简单的JavaScript在Firefox 3.5.2中不起作用 – url在浏览器中更改但页面未导航到锚点.在IE 8中它完美运行.

为什么这个JavaScript代码在Firefox中不起作用?我错过了什么吗?

我已经解决了我的问题.在我的锚点存在之前调用了JavaScript代码.这就是Firefox没有滚动页面的原因.

我的代码现在看起来像tihs:

this.ClientScript.RegisterStartupScript(this.GetType(), "navigate", "window.onload = function() {window.location.hash='#message';}", true);

页面加载后,我正在调用我的简单JavaScript函数.

找到解决方案的关键是克莱顿回答. Firebug报告getElementById返回null引用.然后我查看生成的HTML,就像andrewWinn建议的那样 – 在锚点存在之前调用了JavaScript.做了一点谷歌搜索并找到了解决方案.

谢谢你的回答!

本文共计275个文字,预计阅读时间需要2分钟。

如何使用ASP.NET实现页面导航至特定代码锚点?

我有一个简单的Web窗体,代码如下:

Web窗体

POST后我想导航用戶到我的主播消消页面。

我有一个简单的Web窗体,代码如下:

如何使用ASP.NET实现页面导航至特定代码锚点?

//... //tons of text //... <a name="message" /> //... //tons of text //... <asp:Button ID="ButtonSend" runat="server" text="Send" onclick="ButtonSend_Click" />

POST后我想导航用户到我的主播“消息”.我有以下代码:

protected void ButtonSend_Click(object sender, EventArgs e) { this.ClientScript.RegisterStartupScript(this.GetType(), "navigate", "window.location.hash='#message';", true); }

这个简单的JavaScript在Firefox 3.5.2中不起作用 – url在浏览器中更改但页面未导航到锚点.在IE 8中它完美运行.

为什么这个JavaScript代码在Firefox中不起作用?我错过了什么吗?

我已经解决了我的问题.在我的锚点存在之前调用了JavaScript代码.这就是Firefox没有滚动页面的原因.

我的代码现在看起来像tihs:

this.ClientScript.RegisterStartupScript(this.GetType(), "navigate", "window.onload = function() {window.location.hash='#message';}", true);

页面加载后,我正在调用我的简单JavaScript函数.

找到解决方案的关键是克莱顿回答. Firebug报告getElementById返回null引用.然后我查看生成的HTML,就像andrewWinn建议的那样 – 在锚点存在之前调用了JavaScript.做了一点谷歌搜索并找到了解决方案.

谢谢你的回答!