简体   繁体   English

成功阻止ajax调用后无法阻止链接重定向

[英]Unable to prevent a link to redirect after successful ajax call

I have this HTML code 我有这个HTML代码

<div class="pull-right" id="pagination">
    <ul class="pagination">
        <li><a href="/filter/sector/c2VjXzE?page=1">1</a></li>
        <li><a href="/filter/sector/c2VjXzE?page=2">2</a></li>
        <li><a href="/filter/sector/c2VjXzE?page=3">3</a></li>
        <li><a href="/filter/sector/c2VjXzE?page=4">4</a></li>
    </ul>
</div>

and I am able to replace the code above anytime an ajax call is successful as shown in the snippet below 而且我可以在每次ajax调用成功时替换上面的代码,如下面的代码段所示

 $("#pagination").find("ul a").on('click', function(e) { alert('Link has been clicked'); e.preventDefault(); }); function update() { $.ajax({ dataType: 'json', url: url, data: { page: page_to_visit } }).done(function(json) { var totalPage = json.total; var pagination_links = '<ul class="pagination">'; var i = 1; while (i <= totalPage) { pagination_links = pagination_links + '<li><a href="' + url + '?page=' + i + '">' + i + '</a></li>'; i++; } pagination_links = pagination_links + '</ul>'; $("#pagination").html(pagination_links); }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="pull-right" id="pagination"> <ul class="pagination"> <li><a href="/filter/sector/c2VjXzE?page=1">1</a></li> <li><a href="/filter/sector/c2VjXzE?page=2">2</a></li> <li><a href="/filter/sector/c2VjXzE?page=3">3</a></li> <li><a href="/filter/sector/c2VjXzE?page=4">4</a></li> </ul> </div> 

But the problem I am facing is that, before the ajax call, when I click on the links in the HTML code, I get what is expected. 但是我面临的问题是,在ajax调用之前,当我单击HTML代码中的链接时,我得到了预期的结果。 But after making the ajax call, the links redirect me to another page instead of staying on the page and displaying the alert. 但是在进行ajax调用后,这些链接会将我重定向到另一页面,而不是停留在该页面上并显示警报。

kindly help me solve this problem. 请帮助我解决这个问题。

You need to change your on binding: 你需要改变你on绑定:

$("#pagination").on('click','ul a',function(e) {
 ....
});

So you bind the event to the static element "#pagination" but filter for events in "ul a" by specifying a selector : 因此,您将事件绑定到静态元素"#pagination"但通过指定选择器来过滤"ul a"的事件:

A selector string to filter the descendants of the selected elements that trigger the event. 一个选择器字符串,用于过滤触发事件的所选元素的后代。 If the selector is null or omitted, the event is always triggered when it reaches the selected element. 如果选择器为null或省略,则事件在到达所选元素时始终被触发。

This allows the event to fire even though the elements change, because the event is bound to something that doesn't change, ie "#pagination" . 即使元素发生更改,这也可以触发事件,因为事件绑定到了不变的东西,即"#pagination"

To quote frans comment/good explanation of why this is happening: 引用frans评论/很好地解释为什么会这样:

When you remove elements in the DOM all events bound to them will be destroyed. 当您删除DOM中的元素时,绑定到它们的所有事件都将被销毁。 Even if you replace them with the exact same element again in the exact same location in the DOM. 即使您在DOM中完全相同的位置再次将它们替换为完全相同的元素。

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

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