How do I prevent scrolling to the top of a page when popping up a jQuery UI Dialog?

橙三吉。 提交于 2019-12-18 03:04:32

问题


I currently use jTemplates to create a rather large table on the client, each row has a button that will open a jQuery UI dialog. However, when I scroll down the page and click on one of those buttons, jQuery dialog will open, but the scroll position get lost and the page jumps back to the top (with the blocking and the actual dialog showing off the screen). Has anyone seen or know what might cause this problem?

Thanks.


回答1:


Are you using an anchor tag to implement the "button" that pops the dialog? If so, you'll want the click handler that opens the dialog to return false so that the default action of the anchor tag isn't invoked. If you are using a button, you'd also need to make sure that it doesn't submit (by returning false from the handler) and completely refresh the page.

For example,

$('a.closeButton').click( function() {
     $('#dialog').dialog('open');
     return false;
});


<a class='closeButton'>Close</a>



回答2:


If your buttons work with an html anchor tag with href="#" replace the href for example by href="javascript:;" or any other method that you use to disable the href. The reason why the scrolling happens is because of href="#" scrolls to the top of your page.




回答3:


change your code like this

$('a.closeButton').click( function(e) {
    e.preventDefault();
     $('#dialog').dialog('open');
});



回答4:


You can try :

scrollTo(0, jQuery("body"));


来源:https://stackoverflow.com/questions/1155952/how-do-i-prevent-scrolling-to-the-top-of-a-page-when-popping-up-a-jquery-ui-dial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!