解决fastclick引起的iOS点击select闪退的bug

问题详情:引入fastclick后,在iphone上,轻触select的时候偶尔会出现闪退的现象
解决办法:修改fastclick的源码,判断点击目标的类型是select的话,就return,不再经过fastclick的逻辑。

FastClick.prototype.onTouchStart = function(event) {
    var targetElement, touch, selection;

    // Ignore multiple touches, otherwise pinch-to-zoom is prevented if both fingers are on the FastClick element (issue #111).
    if (event.targetTouches.length > 1) {
        return true;
    }

    targetElement = this.getTargetElementFromEventTarget(event.target);
    touch = event.targetTouches[0];
    ...

var nodeName = targetElement.nodeName.toLowerCase();
var typeAttribute = targetElement.getAttribute('type');
if (nodeName === "select" || (typeAttribute === 'date' && nodeName === "input") || (typeAttribute === 'datetime' && nodeName === "input")){
    return false;
}

后记:

对插件维护和更新来说,改动插件源码的方式不可取。可以在不需要fastclick的元素上加上: class="needsclick"

发表评论

电子邮件地址不会被公开。 必填项已用*标注