微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一键式按钮适用于大屏幕,但在手机设备上只能双击

如何解决一键式按钮适用于大屏幕,但在手机设备上只能双击

这是有角度的。我有一个按钮,只需单击一下,就可以在大屏幕(例如 macbook)上正常工作,但是当它在 iphone 上时,它只能双击才能工作(在 android 上工作正常)。它应该也适用于 oneclick。警报是在单击时触发,但不是在照片上传时触发。 有人可以知道为什么会这样吗?谢谢。

  $scope.triggerUploadPhoto = function () {
        var width = $(window).width();
         alert(123);
         if(width< 768){
             $('#sidebar').css('display','');
             $('#sidebar').addClass('modal-hidden');
             $('#side-modal-upload-photo').removeClass('active-modal');
             $('#side-modal-upload-photo').addClass('modal-hidden');
             $('.five-item-on-hover').css('background-color','transparent');
         }
        $('#photo-upload').trigger('click');
        $('.stored-images').trigger('click');
        $scope.toggleUploadPhoto();
        
    }

解决方法

它适用于 Chrome 浏览器,但有时可能不适用于 safari。最好的方法是使用 jQuery 并使用 touchstart 事件而不是按钮上的 onclick 事件。或者,为了涵盖所有方面,您也可以在元素上使用 ontouchstart 事件。

因此,如果 HTML 代码如下所示,

<html>
    <body>
        <button id="redirectButton">Redirect Me!</button>
    </body>
</html>

然后我们可以在<button>元素中添加这一行

ontouchstart="myFunction()" 并在 JS 中定义您的 myFunction()。但一种更安全的方法(更安全:适用于所有)是使用 jQuery

只需在 html 的 head 部分插入任何 jQuery CDN 并在 JS 中添加这一行并定义元素

$(document).ready(function(){ 
    $('#buttonID').on('click touchstart',function() {
        //Your code here
    });
});

和示例:

/*jQuery - include in JS*/
$(document).ready(function(){ 
  $('#redirectButton').on('click touchstart',function() {
    window.location.href="http://example.com";
  });
});
<!--HTML-->
<html>
    <head>
        <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    </head>
    <body>
        <button id="redirectButton">Take Me To Example.com</button>
    </body>
</html>

如果您使用引导程序但仍然无法正常工作,您可能需要参考此线程:Button not working on Mobile Devices but works on PC bootstrap

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。