如何解决如何单击将执行但不会重定向的链接 手机,平板电脑
根据此解决方案,我做了一个执行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).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。