Javascript无法在移动设备上运行,但适用于桌面设备 [英] Javascript not working on mobile but works on desktop

查看:41
本文介绍了Javascript无法在移动设备上运行,但适用于桌面设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这适用于桌面浏览器,但不适用于我的iOS手机。我尝试添加'touchstart'并查看了

解决方案

答案:它不适用于iOS Safari的原因是因为在我的js页面中我正在使用ES6,特别是'let',[目前不支持] [1]。改为ES5,问题就消失了。

  $('body')。on('click','。dashboard_leftNav_category a' ,function(){
var link = $(this).attr('showSection'); //从let link
var show = $('[section ='+ link +']改变');
$('[section]')。hide();
$('body')。find(show).fadeIn();
$('html,body ')。scrollTop(0);
});


This works on a desktop browser, but not on my iOS mobile phone. I tried adding 'touchstart' and looked at this post's solution to check how other's got it to work, but it still isn't working. Any suggestions as to other options? I also tried adding e.preventDefault() - and added e to function(), but that didn't work as well.

I have tried:

 $('body').on('click touchstart', '.myContainer', function() {
     $(this).toggleClass('myContainer-unselected').toggleClass('myContainer-selected');
 });

Edit: It appears there may be something else going on, I changed the code to be as general as possible and it is not firing the event on iOS, but working in my chrome emulator:

$(document).on('click touchstart', 'body', function() {
      alert('hi');
    });

Additional update:

I have added the following code to my script.js file:

$('body').css('display', 'none');

As expected, the screen goes blank on my desktop browser for both local and on heroku, but when I test on mobile, the screen is not blank. It looks like js isn't working properly.

Images attached:

解决方案

Answer: the reason it wasn't working on iOS Safari is because in my js page I was using ES6, specifically 'let' which is [not supported currently][1]. Changed to ES5 and the issue disappeared.

$('body').on('click', '.dashboard_leftNav_category a', function() {
      var link = $(this).attr('showSection'); //changed from let link
      var show = $('[section="'+link+'"]');
      $('[section]').hide();
      $('body').find(show).fadeIn();
      $('html,body').scrollTop(0);
    });

这篇关于Javascript无法在移动设备上运行,但适用于桌面设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆