简体   繁体   English

移动javascript div隐藏

[英]mobile javascript div hiding

I am trying to use this script to hid a div if you are on a mobile device, and it is not working for me. 如果您在移动设备上,我正在尝试使用此脚本隐藏div,但它对我不起作用。 Is something wrong with my javascript? 我的JavaScript有问题吗?

<script type="text/javascript">
<!--
if (screen.width <= 640) {
document.getElementById('div2').style.display="block";
document.getElementById('div1').style.display="none";
}
//-->
</script>

You should comment the <!-- at the beginning of your code. 您应该在代码的开头注释<!--

But the best way to do what you want would be the media queries: 但是,执行所需操作的最佳方法是媒体查询:

<link rel="stylesheet" media="only screen and (max-device-width: 640px)" href="small-device.css" />

This way you could set a small-device.css file with specific styles for the mobile version of your site. 这样,您可以为网站的移动版本设置具有特定样式的small-device.css文件。

Take a look at this tutorial: http://mobile.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website/ 请看一下本教程: http : //mobile.smashingmagazine.com/2010/07/19/how-to-use-css3-media-queries-to-create-a-mobile-version-of-your-website /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM