张贴后滚动页面返回
本文关键字:返回 滚动 张贴 | 更新日期: 2023-09-27 18:19:47
我想在返回后向下滚动页面。有/没有更新面板,代码就无法工作。
浏览器控制台中的错误:
未捕获的语法错误:意外的标记<VM465:1
当点击VM465:1时,它指向这条线:
<script type="text/javascript">$('html, body').animate({ scrollTop: 1600 }, 'slow');</script>
ASPX:
<asp:UpdatePanel ID="up1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnMore" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:LinkButton ID="btnMore" runat="server" OnClick="btnMore_Click">ShowMore</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
C#:
protected void btnMore_Click(object sender, EventArgs e)
{
//string message = "alert('Hello!')";
//ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "alert", message, true);
ScriptManager.RegisterClientScriptBlock(sender as Control, this.GetType(), "ScrollPage", GetPageScrollScript(900), true);
}
private string GetPageScrollScript(int heightToScroll)
{
string ScrollPage = "<script type='"text/javascript'">$('html, body').animate({ scrollTop: " + heightToScroll + " }, 'slow');</script>";
return ScrollPage;
}
当我取消注释时(带和不带updatepanel),警报会起作用
应该做些什么才能使卷轴正常工作。
更新:它是这样对我起作用的:
ScriptManager.RegisterClientScriptBlock(sender as System.Web.UI.Control, this.GetType(), "ScrollPage", "$('html, body').animate({ scrollTop: " + 1000 + " }, 'slow')", true);
您正在使用类似的警报
alert('Hello!')
这是有效的,但对于你想要执行的脚本,你可以将其包装在标签中
<script type='"text/javascript'">$('html, body').animate({ scrollTop: " + heightToScroll + " }, 'slow');</script>
试着不用它们?
$('html, body').animate({ scrollTop: " + heightToScroll + " }, 'slow')