使用 jquery 在 iPad 上触发触摸事件的正确方法是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10873680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:52:51  来源:igfitidea点击:

what is the proper way to trigger a touch event on the iPad with jquery

jqueryipadtriggers

提问by user278859

I have tried the following (.myviewer is a div)...

我尝试了以下(.myviewer 是一个 div)...

$('.myviewer').click();

  and
$('.myviewer').trigger('touchstart');

  and
$('.myviewer').trigger('click');

All work on a computer but not an iPad. What am I doing wrong?

全部在计算机上工作,但不是 iPad。我究竟做错了什么?

Here is what the body of the html page looks like...

这是 html 页面的正文的样子...

<body>
    <div class="myviewer" onclick="window.open('myPDFFile.pdf');">Programmatically clicked</div>
</body>

And to round this out here is my jquery code...

为了解决这个问题,这是我的 jquery 代码......

$(document).ready(function() {
var isMobile = {
    Android : function() {
        return navigator.userAgent.match(/Android/i) ? true : false;
    },
    BlackBerry : function() {
        return navigator.userAgent.match(/BlackBerry/i) ? true : false;
    },
    iOS : function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
    },
    Windows : function() {
        return navigator.userAgent.match(/IEMobile/i) ? true : false;
    },
    any : function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());                               }
}; 

if(isMobile.any()) {
    $('.myviewer').clck();  //this does works on computers but not on iPad
}else {
    var markup = "<object   data='myPDFFile.pdf#toolbar=1&amp;navpanes=1&amp;scrollbar=0&amp;page=1&amp;view=FitH' type='application/pdf' width='100%' height='100%'> </object>";
    $('.myviewer').append(markup);
};      

});

});

回答by Esailija

For .triggerto do anything, you must bind the event first, which you haven't done. onclick=""doesn't count.

为了.trigger做任何事情,你必须先绑定事件,你还没有完成。onclick=""不算。

To bind the event first use:

要首先绑定事件,请使用:

$(document).ready(function() {
    $('.myviewer').on( "touchstart", function(){
        $(this).remove();
    });

    var isMobile = { //...your original code continues here

Then you can later trigger it:

然后你可以稍后触发它:

$('.myviewer').trigger('touchstart');