如何单击将执行但不会重定向的链接 手机,平板电脑

问题描述

根据此解决方案,我做了一个执行href的按钮,它没有将我重定向页面(我正在开发Web应用程序,当某人单击按钮时,http请求正在运行,并且正在打开应用程序) ,问题是当我在平板电脑上进行测试时,当我触摸按钮时,它会将我重定向页面,而我不知道如何解决此问题。

// Act on clicks to a elements
$("#link1").on('click',function(e) {
    // prevent the default action,in this case the following of a link
    e.preventDefault();
    // capture the href attribute of the a element
    var url = $(this).attr('href');
    // perform a get request using ajax to the captured href value
    $.get(url,function() {
        // success
    });
});
<a id='link1' href="http://www.google.com">Link</a>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

解决方法

如果您不希望它像普通链接一样工作,我将不使用href属性,而是使用诸如data-href之类的更抽象的属性。您仍然可以通过jquery获取该属性,而不必阻止任何默认设置。

,

这是异步操作,不创建新标签页

您应将#用作href并添加data-href //根据对元素的点击进行操作

$("#link1").on('click',function(e) {
    // prevent the default action,in this case the following of a link
    e.preventDefault();
    // capture the href attribute of the a element
    var url = $(this).attr('data-href');
    // perform a get request using ajax to the captured href value
    $.get(url,function() {
        // success
    });
});

和html

<a id='link1' href="#" data-href="http://www.google.com">Link</a>

这是用于创建新标签页的: 更改

$.get(url,function() {
        // success
    });

window.open(url).