简体   繁体   English

停止javascript onclick'page jumping'

[英]Stop javascript onclick 'page jumping'

Can't seem to prevent the jump to top of page behavior. 似乎无法阻止跳转到页面顶部的行为。

I've tried everything suggested here 我已经尝试了这里建议的一切 preventing onclick page jumps 防止onclick页面跳转

What else could be done to stop onclick page jumps? 还有什么办法可以阻止onclick页面跳转?

Thanks. 谢谢。

Here's a snippet of the HTML and JS 这是HTML和JS的片段

HTML:

<a href="javascript: return null;" onclick='javascript:getPosts("page=1&display=all"); return false;'>Everyone</a>


JS:

function getPosts(qry) {
    var thePage = 'posts.php?' + qry;
    myReq.open("GET", thePage, true);
    myReq.onreadystatechange = theHTTPResponse;
    myReq.send(null);
}

function theHTTPResponse() {
    if (myReq.readyState == 4) {
        if(myReq.status == 200){
            var response = myReq.responseText;
            document.getElementById('posts').innerHTML = response;
        }
    } else {
        document.getElementById('posts').innerHTML = '<img src="images/ajax-loader.gif" />';
    }
}

Returning false in the onclick is really the way to go. 在onclick中返回false实际上是要走的路。 If that's not working for you, we need to see code. 如果那不适合你,我们需要查看代码。

As far as I know, the events are already JS so, you dont need to put onclick="javascript:..." . 据我所知,事件已经是JS所以,你不需要把onclick="javascript:..." Try this: 尝试这个:

<a href="javascript: return null;" onclick='getPosts("page=1&display=all"); return false;'>Everyone</a>

href="javascript:void(0);"

<a href="javascript:;" onclick="...">Everyone</a>

注意分号。

this is what i use and it works 这是我使用的,它的工作原理

<a href="javascript:getPosts('page=1&display=all'); return false;">Everyone</a>

When it comes to ajax requests using the function that is tied to the 'onClick' event as the href always solves the page jumping problem for me. 当涉及使用与'onClick'事件关联的函数的ajax请求时,因为href总是为我解决页面跳转问题。

I've always used: 我一直用:

<a href=""></a>

Mostly because I hate that using 主要是因为我讨厌使用

<a href="#"></a>

appends a # to the end of the url. 在网址的末尾附加一个#

Edit: 编辑:

Are you sure it isn't a browser specific problem or preference? 您确定它不是特定于浏览器的问题或偏好吗?

all you need is 所有你需要的是

<a href="javascript:">My Link!</a>

and add whatever other attributes you need to the tag. 并添加标签所需的任何其他属性。

Strangely, the problem was corrected after I deleted the contents of my php script, saved, then reinserted the same content. 奇怪的是,我删除了我的PHP脚本的内容后,问题得到纠正,保存,然后重新插入相同的内容。 Not sure what effect this had but seems to fix the jumping. 不知道这有什么影响,但似乎解决了跳跃。

I guess the issue was never with returning false in onclick. 我想问题永远不会在onclick中返回false。

Thanks for everyone's time! 谢谢大家的时间!

href =“javascript:undefined”应该做的伎俩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM