如何仅在设备基于 iOS 时运行 jQuery 代码

How to run jQuery code only if device is iOS based?

本文关键字:iOS 运行 jQuery 代码 何仅      更新时间:2023-09-26

可能的重复项:
检测设备是否为 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>

我写什么来让它工作,而不是评论?

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
            });
        });
    }
});