JS對於Android和IOS平台的點擊響應的適配

來源:互聯網
上載者:User

標籤:

綜述

最近做項目的時候發現了一個非常奇怪的問題,就是對於click事件的響應。經過測試發現,對於IOS平台,直接監聽click事件可能是沒有響應的,而在Android和PC上則完全沒有問題。所以通過擷取裝置資訊實現了不同平台的不同監聽。

IOS監聽

對於IOS裝置,只監聽click方法可能是沒有響應的。解決方案就是監聽 “touchend click”事件。

而對於Android和PC,則只監聽click事件即可。

平台檢測

我們利用userAgent來檢測平台

 
123456789 if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {    //alert(navigator.userAgent);      alert("iOS");} else if (/(Android)/i.test(navigator.userAgent)) {    //alert(navigator.userAgent);     alert("Android");} else {    alert("PC");};

上面的JS代碼可以檢測三個平台。

實現監聽

我們可以把方法自訂名字,比如

 
12345 function back_click(){        $(".group-names").show();        $(".groups:visible").hide();        $(this).hide();    }

然後跨平台實現監聽

 
123456 /* bind the event */    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {        $(".back").bind({"touchend click":back_click});    }else{        $(".back").bind({"click":back_click});    }

通過以上監聽便沒有問題了。

總結

通過以上方法便可以實現不同平台的監聽。

JS對於Android和IOS平台的點擊響應的適配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.