简体   繁体   English

单击链接时防止跳转到页面顶部

[英]Prevent Jump to Top of Page When Clicking Link

I have been stuck on how to prevent jumping to the top of the page when clicking on a link that has parsing PHP data. 我一直停留在如何防止单击具有解析PHP数据的链接时跳到页面顶部。

I have tried so many AJAX examples off the internet, but unfortunately with no success. 我已经在互联网上尝试了很多AJAX示例,但不幸的是没有成功。

A link looks like this: 链接如下所示:

<a class="bottle" href="index.php?step=<?php echo$step; ?>&bottle=b0"><img class="bottle" src="images/b0.jpg" /></a>

...with there being 113 of these links, each staying on index.php?, but jumping to the top of the page when clicked. ...其中有113个链接,每个链接都保留在index.php?上,但是单击后跳转到页面顶部。

A preview of the program is: http://www.mtschools.net/aurasoma 该计划的预览是: http : //www.mtschools.net/aurasoma

You just need to use the preventDefault(); 您只需要使用preventDefault(); method in javascript to stop the browser from following through with the anchor links href location. javascript中的方法,以阻止浏览器跟踪锚链接href位置。

<script type="text/javascript">
    var links = document.querySelectorAll('.bottle');
    for(var i = 0; i < links; i++){
        links[i].addEventListener('click', function(e){
            e.preventDefault();
            //alternatively return false;

            //the rest of the ajax code here.
        });
    }
</script>

Whatever ajax call you need to make can be done after //the rest of the ajax code here . 您需要进行的任何ajax调用都可以在//the rest of the ajax code here之后完成。

User a link without HREF, and then set CSS cursor to be pointer either with jQuery or CSS. 使用没有HREF的链接,然后使用jQuery或CSS将CSS光标设置为指针。

This to make clickable sort of link. 这使可点击的链接。 Not entire solution to your problem. 不能完全解决您的问题。

But you could also have an anchor with the name, so when you click, you will move to the anchor and perhaps stay on the anchor, instead of the top of the page? 但是,您也可以使用名称作为锚点,因此当您单击时,您将移至该锚点并可能停留在该锚点上,而不是页面顶部?

<script>
    $().ready(function() {
        ('#link').css('cursor', 'pointer');
    });
</script>

<a id="link">Some Text</a>

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

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