jQuery - 根据类显示和隐藏不同的 div
嘿大家, 我正在尝试为我的新网站制作一个投资组合页面,该页面将在网格中显示投资组合项目。它的功能看起来很简单,但我似乎无法确定 jQuery。这就是我需要它工作的方式。
- 默认情况下,我希望显示所有项目。
- 在特定类别按钮上单击我希望它显示相应的 div 并隐藏其余部分。
- 这些 div 具有重叠的类(某些项目适合多个类别)。当单击它们各自的类按钮时,应该显示这些 div。
这是我试图用来让它工作的东西(它看起来很笨重,但我是一个 jQuery 菜鸟):
$(document).ready(function(){
$('#showall').click(function(){
$(".item").show("fast");
})
$('#webtrigger').click(function(){
if ($('.web').is(':visible')) {
$('.web').show('fast');
} else {
$('.illustration.print.logo').hide('fast');
}
return false;
})
$('#logotrigger').click(function(){
if ($(".logo").is(":visible")) {
$(".logo").show("fast");
} else {
$(".illustration.print.web").hide("fast");
}
return false;
})
});
<a href="#" id="showall">show all</a><br/>
<a href="#" id="webtrigger">web</a><br/>
<a href="#" id="illustrationtrigger">illustration</a><br/>
<a href="#" id="printtrigger">print</a><br/>
<a href="#" id="logotrigger">logo</a><br />
<div id="wrapper">
<div class="item web">web</div>
<div class="item illustration">illustration</div>
<div class="item print">print</div>
<div class="item logo">logo</div>
<div class="item web logo">web logo</div>
<div class="item print illustration ">illustration print</div>
<div class="item illustration logo">illustration logo</div>
</div>
任何帮助将不胜感激。 谢谢!
Hey all,
I am trying to make a portfolio page for my new website that will display portfolio items in a grid. The functionality of it seems simple but I can't seem to nail down the jQuery. Here is how I need it to work.
- By default I want all items to be shown.
- On a specific catagory button click I want it to show the respective divs and hide the rest.
- These divs have overlapping classes(some items fit into more then one category). Those divs should be shown when either of their respective class buttons are clicked.
Here is what I was trying to use to make it work(it seems bulky, but I'm a jQuery noob):
$(document).ready(function(){
$('#showall').click(function(){
$(".item").show("fast");
})
$('#webtrigger').click(function(){
if ($('.web').is(':visible')) {
$('.web').show('fast');
} else {
$('.illustration.print.logo').hide('fast');
}
return false;
})
$('#logotrigger').click(function(){
if ($(".logo").is(":visible")) {
$(".logo").show("fast");
} else {
$(".illustration.print.web").hide("fast");
}
return false;
})
});
<a href="#" id="showall">show all</a><br/>
<a href="#" id="webtrigger">web</a><br/>
<a href="#" id="illustrationtrigger">illustration</a><br/>
<a href="#" id="printtrigger">print</a><br/>
<a href="#" id="logotrigger">logo</a><br />
<div id="wrapper">
<div class="item web">web</div>
<div class="item illustration">illustration</div>
<div class="item print">print</div>
<div class="item logo">logo</div>
<div class="item web logo">web logo</div>
<div class="item print illustration ">illustration print</div>
<div class="item illustration logo">illustration logo</div>
</div>
Any help would be greatly appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将从各种控件
id
中删除“触发器”,并添加一个“控件”class
来给出:然后使用 jQuery:
JS Fiddle 演示。
I'd remove the 'trigger' from the various control
id
s, and add a 'control'class
to give:Then use the jQuery:
JS Fiddle demo.
试一试。
它将您在变量中显示的内容存储起来,然后使用
not()
(docs) 将它们从隐藏的内容中排除。Give this a shot.
It stores the ones you're showing in a variable, then uses
not()
(docs) to exclude them from the ones being hidden.我会做这样的事情:
您立即隐藏所有项目,然后用动画显示您感兴趣的 div。
当然,对于您拥有的所有触发器来说都是相同的。
I would make something like this :
You hide all items instantaneously, and then show with animation the divs you are interested in.
Of course, it's the same for all triggers you have.
当您必须挑选项目时,类似这样的事情应该起作用:
Something like this should work when you have to pick through the items:
jQuery:
<代码>
$(document).ready(function(){
});
HTML:
<代码>
显示全部
web
插图
打印
logo
;
;
;
;
<代码>
jQuery:
$(document).ready(function(){
});
HTML:
<a href="#" id="showall">show all
<a href="#" id="web" class="trigger">web
<a href="#" id="illustration" class="trigger">illustration
<a href="#" id="print" class="trigger">print
<a href="#" id="logo" class="trigger">logo
<div id="wrapper">
<div class="item web">web</div>
<div class="item illustration">illustration</div>
<div class="item print">print</div>
<div class="item logo">logo</div>
<div class="item web logo">web logo</div>
<div class="item print illustration ">illustration print</div>
<div class="item illustration logo">illustration logo</div>
</div>
我会做一个技巧,使用链接的 id 来触发内容的类:
要默认显示所有内容,请使用 css
然后使用 jquery 来显示它们:
I would do a trick and use the id of the link to trigger the class of the content:
To show them all by default, use css
Then jquery to display them: