如何在iOS设备上使用touchstart替换点击 [英] How to replace click with touchstart on iOS devices

查看:182
本文介绍了如何在iOS设备上使用touchstart替换点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标

点击时关闭锚定代码的父div。在下面的代码中,我想在用户点击锚定代码 close_performance_tt 时隐藏div performance_tt

To close the parent div of an anchor tag when clicked. In the code below, I want to hide div performance_tt when the user clicks on anchor tag close_performance_tt.

strong>问题

Problem

无法在iOS设备上工作数小时后使用。在其他一切,即使是一个黑莓10设备工作正常。

Unable to get it to work on iOS devices after spending several hours at it. Works fine on everything else, even a BlackBerry 10 device.

<div id="performance_tt" style="display: none;width: 300px;height: 200;overflow: auto;padding: 5px;background-color: yellow;">
    <div>Website performance has become an important consideration for most sites.
    The speed of a website affects usage and user satisfaction, as well as search engine rankings, a factor that directly correlates to revenue and retention.
    As a result, creating a system that is optimized for fast responses and low latency is key.</div>
    <a id="close_performance_tt" href="#">Close</a>
    <script>
    var userAgent = navigator.userAgent.toLowerCase();
    var isiOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false);
    if (isiOS) {
        $("#close_performance_tt").bind('touchstart', function() {
            alert('Touch-start event triggered');
        });
    } else {
        $("#close_performance_tt").bind('click', function() {
            alert('Click event triggered');
        });
    }
    </script>
</div>


推荐答案

定义一个可以在以后使用的clickhandler: / p>

Define a clickhandler that you can use later on:

var clickHandler = ('ontouchstart' in document.documentElement ? "touchstart" : "click");

$("a").bind(clickHandler, function(e) {
    alert("clicked or tapped. This button used: " + clickHandler);
});

这将触发点击非触摸设备和touchstart触摸设备。

This will trigger click on non-touch devices and touchstart on touch devices.

当这样说时,我强烈建议您改用快速点击,事件。使用上述解决方案,您将在链接上触发touchstart,当您在其上滑动时滚动页面,例如 - 这是不理想的。

When that is said, I will strongly recommend using Fast click instead, and use regular click-events. With the above solution, you will trigger "touchstart" on links when you swipe on it to scroll the page for instance - which is not ideal.

这篇关于如何在iOS设备上使用touchstart替换点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆