简体   繁体   English

隐藏没有特定属性的DOM元素

[英]Hide DOM elements which do not have a specific attribute

I want to hide all images which have no data-web-src attribute on 767px. 我想隐藏所有在767px上没有data-web-src属性的图像。 I tried the following but I failed; 我尝试了以下方法,但失败了; how can I do that? 我怎样才能做到这一点?

 $('#homepage-carousel .lazy_res').each(function(index, value) { var ws = $(window).width(); var large = 1024; var medium = 767; var small = 0; if (ws <= medium) { $(this).not('[data-web-src]').hide(); } else { $(this).not('[data-web-src]').show(); } }); 
 img { width: 500px; float: left; margin-right: 10px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="homepage-carousel"> <img class="lazy_res" src="http://pre07.deviantart.net/338a/th/pre/i/2012/007/f/7/mapa_mundi_com_bandeiras___preto_by_plamber-d4leocd.jpg" alt="" /> <img class="lazy_res" src="http://img05.deviantart.net/a6be/i/2013/099/8/9/helena_harper_by_plamber-d6125tx.jpg"> </div> 

Codepen Demo Codepen演示

This should be done with CSS Media Queries . 这应该通过CSS Media Queries完成 No JavaScript required. 无需JavaScript。

 /* Set page default styles and styles that should only be in effect for viewports under 767px wide here. */ img { width: 500px; float: left; margin-right: 10px; } /* Apply the following CSS only to viewports wider than 767px */ @media (min-width: 767px) { /* Select all images except those with an attribute of: dat-web-src */ img:not([data-web-src]) { display: none; /* Hide the matching elements */ } /* Make any other CSS changes you like here */ /* This class will only be applied to images when the media query is in effect. */ img.someNewClass { /* Whatever you need here */ } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="homepage-carousel"> <img class="lazy_res" src="http://localhost:82/works/anitur/img/slider/1.jpg" alt="" /> <img class="lazy_res" src="http://localhost:82/works/anitur/img/assets/mice-1.jpg"> </div> 

You need to set your code within a function and then it can be called onload and also onresize to test it: 您需要在函数中设置代码,然后可以将其称为onload onresize进行测试:

see https://api.jquery.com/on/ 参见https://api.jquery.com/on/
Description: Attach an event handler function for one or more events to the selected elements. 说明:将一个或多个事件的事件处理程序功能附加到所选元素。

 function testit() { $("#homepage-carousel .lazy_res").each(function(index, value) { var ws = $(window).width(); var large = 1024; var medium = 767; var small = 0; if (ws <= medium) { $(this).not('[data-web-src]').hide(); } else { $(this).not('[data-web-src]').show(); } }); } $(window).on('resize load', testit ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> below 767px wide, you'll see nothing else here :), use the full page mode to test the snippet. then resize the window <div id="homepage-carousel"> <img class="lazy_res" src="http://pre07.deviantart.net/338a/th/pre/i/2012/007/f/7/mapa_mundi_com_bandeiras___preto_by_plamber-d4leocd.jpg" alt="" /> <img class="lazy_res" src="http://img05.deviantart.net/a6be/i/2013/099/8/9/helena_harper_by_plamber-d6125tx.jpg"> </div> 

https://codepen.io/gc-nomade/pen/EXLZEN https://codepen.io/gc-nomade/pen/EXLZEN

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

相关问题 仅在禁用了属性的情况下选择dom中的元素 - Select elements in the dom only if they have the attribute disabled 如何隐藏没有 src 属性的 img 元素,直到添加该属性? - How to hide img elements that do not have src attribute, until the attribute is added? HTML 元素是否具有 DOM 的“隐藏索引”? - Do HTML elements have 'hidden indexes' for the DOM? 如何使用jquery查找并选择具有以特定单词开头的数据属性的所有元素? - How can I find and select all elements which have data attribute start with specific word with using jquery? 是否可以将具有特定值的所有元素放入任何类型的属性中? - Is it possible to get all elements which have a specific value into any type of attribute? 如何隐藏 DOM 元素 - How to hide DOM elements 在 DOM 元素中声明数据集属性的哪种方式更适合 React? - Which way of declaring dataset attribute in DOM elements are better for React? 计算具有特定属性的子代数 - Count the number of children which have a specific attribute jQuery-选择在each()循环中具有“隐藏”属性的DOM元素 - jQuery - select DOM elements that have “hidden” attribute in an each() loop 如何选择具有以下内容的HTML DOM元素:hover子类? - How to select HTML DOM elements which have :hover subclass?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM