最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Hide all elements except the given data-* attribute with jQuery - Stack Overflow

programmeradmin2浏览0评论

I'm making a menu if someone clicks on one object it should filter all of them accordingly (i.e: all projects, pleted projects a.s.o. I have a jQuery to take care of this like this (I added the .not() recently, before adding it this script worked):

$("#pleted").click(function(){
 $('.project_wrapper[data-category="pleted_projects"]').not(this).hide();
});

I have figured out that I should use .not() to .hide all objects that don't have the given [data-category] or am I doing this wrong?


Edit

The HTML:

The Menu:

<ul class="project_menu>
 <li id="plete">Completed Projects</li>
</ul>

The Projects:

<div class="project_wrapper" data-category="pleted_projects">The projects</div>


Edit Got it working thanks to @Nitha & @Sam Hollenbach thanks!

Edited a bit myself but here is the final jQuery code I've got:

jQuery(document).ready(function($){

 // Show all
 $("#all").click(function(){
   $(".project_wrapper").show();
 });

 // Complete
 $("#plete").click(function(){
   $(".project_wrapper:not([data-category='pleted_projects'])").hide();
   $(".project_wrapper[data-category='pleted_projects']").show();
 });

});

Update

Instead of using .show and .hide I used .css("visibility", "collapse") & .css("visibility", "visible") show and hide seemed to bug out for me in WordPress.

I'm making a menu if someone clicks on one object it should filter all of them accordingly (i.e: all projects, pleted projects a.s.o. I have a jQuery to take care of this like this (I added the .not() recently, before adding it this script worked):

$("#pleted").click(function(){
 $('.project_wrapper[data-category="pleted_projects"]').not(this).hide();
});

I have figured out that I should use .not() to .hide all objects that don't have the given [data-category] or am I doing this wrong?


Edit

The HTML:

The Menu:

<ul class="project_menu>
 <li id="plete">Completed Projects</li>
</ul>

The Projects:

<div class="project_wrapper" data-category="pleted_projects">The projects</div>


Edit Got it working thanks to @Nitha & @Sam Hollenbach thanks!

Edited a bit myself but here is the final jQuery code I've got:

jQuery(document).ready(function($){

 // Show all
 $("#all").click(function(){
   $(".project_wrapper").show();
 });

 // Complete
 $("#plete").click(function(){
   $(".project_wrapper:not([data-category='pleted_projects'])").hide();
   $(".project_wrapper[data-category='pleted_projects']").show();
 });

});

Update

Instead of using .show and .hide I used .css("visibility", "collapse") & .css("visibility", "visible") show and hide seemed to bug out for me in WordPress.

Share Improve this question edited Dec 17, 2018 at 13:59 Krullmizter asked Dec 17, 2018 at 8:55 KrullmizterKrullmizter 5691 gold badge9 silver badges31 bronze badges 1
  • 2 It would help to see your actual HTML too – Rory McCrossan Commented Dec 17, 2018 at 8:57
Add a ment  | 

2 Answers 2

Reset to default 5

The below code will hide all the project_wrapper div with data-category not equal to "pleted_projects" and will show the project_wrapper div with data-category equal to "pleted_projects"

$(".project_wrapper:not([data-category='pleted_projects'])").hide();
$(".project_wrapper[data-category='pleted_projects']").show();

I believe what you're asking is to hide all elements within .project_wrapper except for the .project_wrapper[data-category="pleted_projects"] element. In that case I believe you can do this

$('.project_wrapper *').not('.project_wrapper[data-category="pleted_projects"').hide()​;

Or if you want to remove everything in the body

$('body *').not('.project_wrapper[data-category="pleted_projects"').hide()​;

This will remove all elements within .project_wrapper or body, subtract the one with the correct data-category, and then hide all the others.

Source

发布评论

评论列表(0)

  1. 暂无评论