执行JQuery脚本时页面滚动到顶部

时间:2022-03-18 15:28:38

can you please help me out? Can't get this working and it's driving me nuts.

你可以帮帮我吗?不能让这个工作,这让我疯了。

I have a GridView inside an UpdatePanel. One of the links in the gridview uses the following JQuery script:

我在UpdatePanel中有一个GridView。 gridview中的一个链接使用以下JQuery脚本:

ScriptManager.RegisterClientScriptBlock(this, GetType(), "Script1", "$('#divid1').click(function () {$('#divid2').toggle('slow');});", true);

So I am registering the script like this and it works fine. The issue I am having is that everytime I click on the link, the page scrolls to top. How I can execute the JQuery script onclick without the page scrolling to top?

所以我正在注册这样的脚本,它工作正常。我遇到的问题是,每次点击链接,页面都会滚动到顶部。如何在没有页面滚动到顶部的情况下执行JQuery脚本onclick?

Thanks a lot!

非常感谢!

3 个解决方案

#1


1  

if you have # in your href attribute the browsers usually do jump to top. see if you have this problem and your code has a # in href

如果你的href属性中有#,浏览器通常会跳转到顶部。看看你是否有这个问题,你的代码有一个#in href

#2


0  

$('divid1').click(function (e) {
    e.preventDefault();
    // your other code
});

#3


0  

Because you are using `ScriptManager.RegisterClientScriptBlock ` this will go server side so scroll is going to top.

Instead of this use Jquery for link click event

$('divid1').click(function (e) {
    e.preventDefault();
    // your other code
});

#1


1  

if you have # in your href attribute the browsers usually do jump to top. see if you have this problem and your code has a # in href

如果你的href属性中有#,浏览器通常会跳转到顶部。看看你是否有这个问题,你的代码有一个#in href

#2


0  

$('divid1').click(function (e) {
    e.preventDefault();
    // your other code
});

#3


0  

Because you are using `ScriptManager.RegisterClientScriptBlock ` this will go server side so scroll is going to top.

Instead of this use Jquery for link click event

$('divid1').click(function (e) {
    e.preventDefault();
    // your other code
});