如何检测移动浏览器并在检测到移动浏览器时显示特定的Div元素

时间:2017-08-18 07:02:33

标签: jquery responsive

我有一个使用ASP.NET MVC构建的响应式Web应用程序。我有两个Div元素的特定条件。我想检测用户是否在移动设备上显示用于移动屏幕的第一部分,如果没有显示用于桌面屏幕的第二部分。我尝试使用detectmobilebrowsers.com,但我无法弄清楚如何使用它,虽然我经历了很多SO问题。

到目前为止我尝试了什么:

<div class="col-md-6" style="padding:0px;">             
<div id="carousel-example-generic" class="carousel slide" data-    
ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" 
class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
</ol>

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">  

<div class="item active">
  <img src="images/slider/1.jpg" alt="blue">      
</div>

<div class="item">
  <img src="images/slider/2.jpg" alt="purple">     
</div>

<div class="item">
  <img src="images/slider/3.jpg" alt="red">     
</div>

<div class="item">
  <img src="images/slider/4.jpg" alt="red">     
</div>

<div class="item">
  <img src="images/slider/5.jpg" alt="red">     
</div>  
</div>

<!-- Controls -->
<a class="left carousel-control" href="#carousel-example-generic" 
role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#carousel-example-generic" 
role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

删除内联样式属性

style="display:none"

并尝试在样式标记中添加此css

#mobile{display:none;}
@media only screen and (max-width: 550px) { #desktop{display:none;} #mobile{display:block;} }
@media only screen and (max-width: 480px) { #desktop{display:none;} #mobile{display:block;} }