简体   繁体   English

如果屏幕小于 767px (jQuery),则隐藏特定的 HTML 元素

[英]Hide specific HTML element if screen is smaller than 767px (jQuery)

On one of my websites I want to remove a specific HTML element if the screen is smaller than 767px (on mobile devices).在我的一个网站上,如果屏幕小于 767 像素(在移动设备上),我想删除特定的 HTML 元素。

The following piece of HTML is used 9 times on the page:下面这张 HTML 在页面上被使用了 9 次:

<div class="price-list__item-desc">&nbsp;</div>

So all 9 pieces of HTML have to be removed only on mobile devices.因此,只有在移动设备上才能删除所有 9 个 HTML。

I already included the following jQuery file on the website:我已经在网站上包含了以下 jQuery 文件:

jQuery(function ($) {
    if (window.matchMedia('(max-width: 767px)').matches) {
        $('<div class="price-list__item-desc">&nbsp;</div>').hide();
    }
    else {
        $('<div class="price-list__item-desc">&nbsp;</div>').show();
    }
});

However, the code is not working and the pieces of HTML still show up on mobile devices.但是,代码不起作用,HTML 的片段仍然出现在移动设备上。 I know there might be a very easy fix for this particular objective.我知道对于这个特定目标可能有一个非常简单的解决方案。 But after searching the internet for two days I have come across so many different options that actually made me more confused.但是在互联网上搜索了两天后,我遇到了很多不同的选择,这实际上让我更加困惑。

Would very much appreciate any help, thanks in advance!非常感谢任何帮助,在此先感谢!

In the first method, when the page width changes, the div.price-list__item-desc element is hidden or shown using jQuery by controlling the page width .在第一种方法中,当页面width发生变化时,通过控制页面width ,使用 jQuery 隐藏或显示div.price-list__item-desc元素。

In the method you developed, the anonymous function is not run to hide the item when the page changes;在您开发的方法中,没有运行匿名function以在页面更改时隐藏项目; you need to use event handler method.您需要使用事件处理程序方法。 In the structure I developed, the onresize event is affected when the page width changes.在我开发的结构中,页面宽度变化时会影响onresize事件。 This way I can catch this event when the page width changes.这样我可以在页面宽度发生变化时捕捉到这个事件。

 $( document ).ready(function() { window.onresize = function() { console.log(`Width: ${$( window ).width()}`); var divs = document.querySelectorAll("div.price-list__item-desc"); if (window.matchMedia('(max-width: 767px)').matches) { $('div.price-list__item-desc').removeClass("active"); for (var i = 0; i < divs.length; i++) { if(divs[i].innerHTML === '&nbsp;') divs[i].style.display = "none"; else divs[i].style.display = "block"; } } else { $('div.price-list__item-desc').addClass("active"); } } window.onresize(); });
 div.price-list__item-desc { background-color: red; display: none; }.active{ display: block;important; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">1</div> <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">2</div> <div class="price-list__item-desc">&nbsp;</div>

there's nothing calling your function.没有什么可以调用您的 function。 Use media queries instead.改用媒体查询。

UPDATE: I added some js to solve your problem.更新:我添加了一些js来解决你的问题。 If you are trying to hide (so the div still takes up space but doesn't show anything) then your question doesn't really make sense.如果您试图隐藏(因此 div 仍然占用空间但不显示任何内容),那么您的问题实际上没有任何意义。 If you want to remove the div then set display to none.如果要删除 div,请将 display 设置为 none。 The js is used to grab the divs that you want js用来抓取你想要的div

 let divs = document.getElementsByClassName('price-list__item-desc') for(let i = 0; i < divs.length; i++){ if (divs[i].innerHTML == "&nbsp;")divs[i].classList.add('hide') }
 @media (max-width: 767px) {.hide{ display:none;} }
 <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">&nbsp;</div> <div class="price-list__item-desc">6</div> <div class="price-list__item-desc">7</div> <div class="price-list__item-desc">8</div> <div class="price-list__item-desc">9</div>

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

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