简体   繁体   English

隐藏 ”<li> ” 元素,如果没有与数据过滤器相关的数据类别</li>

[英]Hide “<li>” element if there is no data-category related to data-filter

Please see this picture first:请先看这张图:

在此处输入图像描述

I have added the video in a div but if I remove this div then <li data-filter="4" id="videos">Videos</li> should get hidden and if I add a video then <li data-filter="4" id="videos">Videos</li> should be visible.我已经在 div 中添加了视频,但是如果我删除了这个 div,那么<li data-filter="4" id="videos">Videos</li>应该被隐藏,如果我添加了一个视频,那么<li data-filter="4" id="videos">Videos</li>应该可见。

How is that possible with JavaScript or jQuery? JavaScript 或 jQuery 怎么可能?

 <ul class="simplefilter"> <li class="active" data-filter="all">All</li> <li data-filter="1">Light decoration</li> <li data-filter="2">Water supply</li> <li data-filter="3">Mobile repairing</li> <li data-filter="4" id="videos">Videos</li> </ul> <div class="col-sm-4 col-xs-6 filtr-item" data-category="4"> <div class="hover ehover14"> <a href="gallary/1.mp4" class="swipebox" title="Branded accessories."> <video controls class="img-responsive img-size"> <source src="gallary/1.mp4" type="video/mp4"> Sorry, your browser doesn't support embedded videos. </video> </a> </div> </div>

To achieve your goal you can loop through each data-filter element and check for matching elements with the same value in their data-category .为了实现您的目标,您可以遍历每个data-filter元素并检查其data-category中具有相同值的匹配元素。 If there are none, hide the original element.如果没有,则隐藏原始元素。 Try this:尝试这个:

 $('li[data-filter]:not(.all)').each((i, el) => { let hasVideos = $(`div[data-category="${el.dataset.filter}"]`).length;= 0. $(el);toggle(hasVideos); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="simplefilter"> <li class="all active" data-filter="all">All</li> <li data-filter="1">Light decoration</li> <li data-filter="2">Water supply</li> <li data-filter="3">Mobile repairing</li> <li data-filter="4" id="videos">Videos</li> </ul> <div class="col-sm-4 col-xs-6 filtr-item" data-category="4"> <div class="hover ehover14"> <a href="gallary/1.mp4" class="swipebox" title="Branded accessories."> <video controls class="img-responsive img-size"> <source src="gallary/1.mp4" type="video/mp4"> Sorry, your browser doesn't support embedded videos. </video> </a> </div> </div>

you can check file url if file exist then display otherwise hide this block您可以检查文件 url 如果文件存在则显示,否则隐藏此块

$.ajax({
url:'http://www.your-file-url',
type:'HEAD',
error: function()
{
    //file not exists
},
success: function()
{
    //file exists
}

}); }); enter code here

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

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