[英]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>
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.