1

我有一个能够独立运行的移动网络应用程序。为了防止在 Safari 中打开 href 并将它们保留在 webapp 中,我做了:

if (("standalone" in window.navigator) && window.navigator.standalone) {
        // For iOS Apps
        $('a').on('click', function(e){
                    e.preventDefault();
                    var new_location = $(this).attr('href');
                    if (new_location != undefined && new_location.substr(0, 1) != '#' && $(this).attr('data-method') == undefined){
                        window.location = new_location;
                    }
        });
    }

但是,这也使现有的单击事件无法运行,这些事件绑定到 a-tags。

所以我尝试了这个,但它不起作用:

if (("standalone" in window.navigator) && window.navigator.standalone) {
        // For iOS Apps
        $('a').on('click', function(e){
                var ev = $._data(this, 'events');
                if(!ev && !ev.click) {
                    e.preventDefault();
                    var new_location = $(this).attr('href');
                    if (new_location != undefined && new_location.substr(0, 1) != '#' && $(this).attr('data-method') == undefined){
                        window.location = new_location;
                    }
                }
        });
    }

任何帮助表示赞赏!

4

1 回答 1

0

我知道这是一个老问题,所以也许你已经解决了它,但无论如何我都会回答,以防它帮助别人。将自动事件取消进一步向下移动应该可以解决问题。

所以:

if (("standalone" in window.navigator) && window.navigator.standalone) {
    // For iOS Apps
    $('a').on('click', function(e){
        var new_location = $(this).attr('href');
        if (new_location != undefined && new_location.substr(0, 1) != '#' && $(this).attr('data-method') == undefined){
            window.location.href = new_location;
            return false; // Cancel the usual link-opening
        }
        // If the 'if' block above didn't run, process the click as normal
    });
}
于 2015-09-22T19:13:31.967 回答