javascript - How would I detect touch screens in jquery and hide a div - Stack Overflow

How would I use this code:function is_touch_device() {return !!('ontouchstart' in window)

How would I use this code:

function is_touch_device() {
  return !!('ontouchstart' in window) // works on most browsers 
      || !!('onmsgesturechange' in window); // works on ie10
}; 

to detect touch screens and hide a set of divs with the same class.

How would I use this code:

function is_touch_device() {
  return !!('ontouchstart' in window) // works on most browsers 
      || !!('onmsgesturechange' in window); // works on ie10
}; 

to detect touch screens and hide a set of divs with the same class.

Share Improve this question edited Sep 9, 2013 at 3:35 Thaddeus Albers 4,1925 gold badges36 silver badges43 bronze badges asked Feb 13, 2013 at 15:45 Ashley BriscoeAshley Briscoe 7373 gold badges12 silver badges34 bronze badges 7
  • 3 if (is_touch_device()) $('.myclass').hide(); ? – Denys Séguret Commented Feb 13, 2013 at 15:46
  • Do I miss something in your question ? – Denys Séguret Commented Feb 13, 2013 at 15:47
  • I think that will work I will try. I am still getting to grips with jquery and I know I should learn more before trying something this ambitious but I learn better by doing then seeing it in action. – Ashley Briscoe Commented Feb 13, 2013 at 15:50
  • 2 WARNING: I've been bitten by onmsgesturechange, it's available on IE10 even if the device is NOT a touch device, it can't be trusted to reliably determine if a device is touch or not! – Rob Commented Jul 9, 2013 at 12:21
  • 1 just check 'ontouchend' in document. – vsync Commented Feb 19, 2014 at 16:44
 |  Show 2 more ments

2 Answers 2

Reset to default 5

You would simply call the function and use basic logic.

if (is_touch_device()) {
  $('.yourclass').hide();
}

It would be implemented like this:

window.onload=function(){
  if (is_touch_device()){
    var divs=document.getElementsByClassName( 'yourclassname');
    for (var i=0; i<divs.length; i++)
      divs[i].style.display='none'; 
  }
}

function is_touch_device() {
  return !!('ontouchstart' in window) // works on most browsers 
      || !!('onmsgesturechange' in window); // works on ie10
};

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745062857a4609073.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信