取消内联Onclick的浏览器滚动重置 [英] Cancel Browser Scroll Reset for Inline Onclick

查看:112
本文介绍了取消内联Onclick的浏览器滚动重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用原型通过链接的内联onclick构造ajax请求.该函数在页面上显示一个元素,并且一切正常,只是在每次单击链接后浏览器滚动条会重置为顶部. b/c不好,有时我要显示的内容在页面上部分向下滚动,因此使其无法显示.我意识到最好不要包含onclick内联,但是我遇到了这样的情况:我动态创建这些链接,然后缓存html,这样可以更快地实现.有什么方法可以使用嵌入式onclick取消此浏览器滚动重置?这是我的代码:

I'm using prototype to construct an ajax request from an inline onclick on a link. The function shows an element on the page and this all works fine, except that the browser scroll bar resets to the top after every click of the link. This is bad b/c sometimes the content that I want to show is partially scrolled down the page and this makes it so the user cannot see it. I realize it's better to not include the onclick inline, but I have a situation where I dynamically create these links and then cache the html so it is faster this way. Is there any way to cancel this browser scroll reset using an inline onclick? This is what my code looks like:

<a href="#" rel="nofollow" onclick="linkAjax('example')" id="word_link_example">example</a>

function linkAjax(word){ 
  if(!making_ajax_request){
    making_ajax_request = true;
    new Ajax.Request('/example/test',
     {parameters:{data: word},  
      onLoading: function(){searchLoading();},  
      onComplete: function(){
        making_ajax_request = false;
      }
    });
  } else { }
}

推荐答案

您的onclick需要返回false,以阻止浏览器重新加载页面(导致滚动到顶部).试试:

Your onclick needs to return false to stop the browser from reloading the page (resulting in scrolling to the top). Try:

<a href="#" rel="nofollow" onclick="linkAjax('example'); return false;" id="word_link_example">example</a>

这篇关于取消内联Onclick的浏览器滚动重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆