javascript - 针对 Mobile + CSS 类的 jQuery?

标签 javascript jquery css class header

我目前正在使用以下方法从某些包含 .iframe-nav 的页面中删除我的标题

$( document ).ready(function() {
 if($(".iframe-nav").length > 0) // If .iframe-nav exists
    $("#main-header").hide(); // Hide #main-header
});

这很好用。我的问题是我有其他页面包含不受该脚本影响的“.iframe1”,因此在移动设备上显示不正确。由于我现在不会讨论的原因,我需要两个单独的 CSS 类。

有没有办法从包含 .iframe1 的页面以及我的移动网站@mobile only screen and (max-width: 767px)

我尝试了以下方法来测试从所有移动页面中删除#main-header(作为测试),但没有成功。我还有很多东西要学... 无效:

$( document ).ready(function() {
 if (matchMedia('only screen and (max-width: 767px)').matches) {
   $("#main-header").hide(); // Hide #main-header
}

非常感谢。

最佳答案

我建议将逻辑放入 $(window).on("resize" block 中,如下所示:

$(window).on("resize", function (e) {
    var newWindowWidth = $(window).width();
    if(newWindowWidth < 767) //check for width
    {
       $("#main-header").hide();
    }
});

关于javascript - 针对 Mobile + CSS 类的 jQuery?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961432/

相关文章:

javascript - Leaflet.Geosearch : get lon/lat from address

javascript - 提交ajax返回的表单

html - 更改 Bootstrap 模式的默认宽度

javascript - jQuery 使用 Prototype 对象扩展对象

javascript - FancyBox 上的关闭按钮不起作用

javascript - 如何在 Javascript 中创建一个继承另一个函数的属性和方法的函数?

javascript - 使用 jquery 在不同场景下显示所有点击元素

javascript - jquery/javascript : function(e){. ... e 是什么?为什么需要它?它实际上做了什么/完成了什么?

html - Bootstrap 中的导航栏崩溃不起作用

html - 如何添加内嵌背景图片?