【问题标题】:One click button works on larger screens, but on phone devices only double click works一键式按钮适用于大屏幕,但在手机设备上只有双击有效
【发布时间】:2021-05-02 12:06:39
【问题描述】:

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

【问题讨论】:

    标签: javascript css button onclick double-click


    【解决方案1】:

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

    所以,如果 HTML 代码如下所示,

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

    那么我们可以在&lt;button&gt;元素中加入这一行

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      相关资源
      最近更新 更多