如果设备是基于iOS的,如何运行jQuery代码?

时间:2012-08-17 13:09:25

标签: javascript jquery html ios

  

可能重复:
  Detect if device is iOS

这是我想要做的:

<script type="text/javascript">
        // Run This code if device is iOS based
        window.addEventListener('DOMContentLoaded',function() {
            $("body").queryLoader2({
                barColor: "#37201b",
                backgroundColor: "#c2a875",
                percentage: true,
                barHeight: 5,
                completeAnimation: "grow"
            });
       });
        // if it is not iOS run this code
        $(document).ready(function () {
            $("body").queryLoader2({
                barColor: "#37201b",
                backgroundColor: "#c2a875",
                percentage: true,
                completeAnimation: "grow",
                barHeight: 5
            });
        });
        </script>

而不是评论我写的是什么让它起作用?

1 个答案:

答案 0 :(得分:3)

jQuery(document).ready(function($){
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iPad|iPhone|iPod)/i);
    if (agentID) {     
        window.addEventListener('DOMContentLoaded',function() {
            $("body").queryLoader2({
                barColor: "#37201b",
                backgroundColor: "#c2a875",
                percentage: true,
                barHeight: 5,
                completeAnimation: "grow"
            });
       });     
    }
    else
    {
        $(document).ready(function () {
            $("body").queryLoader2({
                barColor: "#37201b",
                backgroundColor: "#c2a875",
                percentage: true,
                completeAnimation: "grow",
                barHeight: 5
            });
        });
    }
});