繁体   English   中英

如果在手机/平板电脑上,则仅显示“隐藏/显示”按钮

[英]Only display Hide/Show button if on Mobile/Tablet

我有以下演示http : //jsfiddle.net/NCj2u/

目前,它可以在所有平台上使用,但是如果显示按钮和隐藏内容仅在平板电脑和移动设备上可以使用,我更喜欢。 桌面将自动显示内容并隐藏按钮。

有人可以解释我该怎么做吗? 我会使用媒体查询还是在Javascript中执行此操作?

我的jQuery

$(document).ready(function() {
  $('.nav-toggle').click(function(){
    //get collapse content selector
    var collapse_content_selector = $(this).attr('href');                   

    //make the collapse content to be shown or hide
    var toggle_switch = $(this);
    $(collapse_content_selector).toggle(function(){
      if($(this).css('display')=='none'){
                        //change the button label to be 'Show'
        toggle_switch.html('Show');
      }else{
                        //change the button label to be 'Hide'
        toggle_switch.html('Hide');
      }
    });
  });

}); 

谢谢。

嗯,您可以尝试几种选择。 我个人建议不要使用浏览器检测,除非您只想专门针对允许您实施的浏览器。 根据您的要求,为什么不尝试媒体查询?

 .nav-toggle{
   display : none; // display none for everyone
 }

/* Landscape phone to portrait tablet  show the button */
@media (max-width: 767px) {

 .nav-toggle{
   display : block; // or inline-block or inline : which ever is appropriate for you.
 }

}

使用这些脚本,您可以检测到任何移动浏览器
http://detectmobilebrowsers.com/
我认为最短的解决方案是要求使用mobil浏览器,并且如果用户未使用该浏览器, click()执行按钮的click()函数

暂无
暂无

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

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