How would I detect touch screens in jquery and hide a div

自作多情 提交于 2020-01-05 06:57:09

问题


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.


回答1:


You would simply call the function and use basic logic.

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



回答2:


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


来源:https://stackoverflow.com/questions/14857320/how-would-i-detect-touch-screens-in-jquery-and-hide-a-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!