jQuery 基于移动或桌面

jQuery based on mobile or desktop

所以我有以下 js:

 jQuery('.rhmps_aj').click(function(e) {
    ...
    success: function(data){
        //Desktop
        jQuery('.desktop_less').hide();
        jQuery('.desktop_more').show();
        //Mobile
        jQuery('.mobile_less').hide();
        jQuery('.mobile_more').show();

 });

有人能告诉我如何根据内容是通过桌面还是移动设备查看来 "disable" 某些功能吗?

例如,对于桌面,我想禁用 "mobile" 功能,反之亦然。

您可以查看屏幕尺寸的宽度。如果屏幕尺寸小于该宽度,您可以执行所需的功能。

示例:

if ($(window).width() < 600) {       // if width is less than 600px
   MobileFunctions();                 // execute mobile function
}
else {                              // if width is more than 600px
   DesktopFunctions();               // execute desktop function
}

我认为 screen.width 可能会成功。

$<!DOCTYPE html>
<html>

<head>
  <script data-require="jquery@2.1.4" data-semver="2.1.4" src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>

<body>
  <script>
    var width = screen.width;
    $(document).ready(function() {
      if (width < 600)
        runMobile();
      else
        runDesktop();
    });

    var runMobile = function() {
      alert("Mobile");
    }

    var runDesktop = function() {
      alert("Desktop");
    }
  </script>
</body>

</html>