首页 > 解决方案 > 一键式按钮适用于大屏幕,但在手机设备上只有双击有效

问题描述

这是有角度的。我有一个按钮,只需单击一下即可在大屏幕(例如 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();
        
    }

标签: javascriptcssbuttononclickdouble-click

解决方案


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

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

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

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

ontouchstart="myFunction()"myFunction()并在 JS 中定义你的。但是一种更安全的方法(更安全:适用于所有)是使用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


推荐阅读