duanrenchuo9244 2014-09-04 23:36
浏览 60

单击“链接”时阻止跳转到页面顶部

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.

I have tried so many AJAX examples off the internet, but unfortunately with no success.

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.

A preview of the program is: http://www.mtschools.net/aurasoma

  • 写回答

2条回答 默认 最新

  • douqingzhi0980 2014-09-04 23:54
    关注

    You just need to use the preventDefault(); method in javascript to stop the browser from following through with the anchor links href location.

    <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.

    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀