[英]I want to have links that can search through my images
I have "tags" on my webpage and once clicked you can see the corresponding image. 我的网页上有“标签”,单击后即可看到相应的图像。 Example: I click on the Dogs link and I see images of Dogs on my gallery. 示例:我单击“狗”链接,然后在图库中看到“狗”的图像。 Something like this: 像这样:
<a href="#" rel="dog">Show Dogs</a>
<script>
$('#filter a').click(function() {
if($(this).attr('rel')) {
$('img').hide().filter('[class="' + $(this).attr('rel') + '"]').show();
$('img').show();
}
return false;
}); });
<img src="http://www.petliferadio.com/doggydog.jpg" class="dog">
however, it doesn't work for me. 但是,它对我不起作用。 Would there be easier/ more affective ways of doing it? 会有更容易/更有情感的方式来做到这一点吗?
Check for empty value .. 检查空值..
if($(this).attr('rel')) {
should be 应该
if($(this).attr('rel') != '') {
$('img').hide();
$('.' + $(this).attr('rel')).show();
}
Hide all images and then show only required images using class selector in jquery. 隐藏所有图像,然后使用jquery中的类选择器仅显示所需的图像。
$('#filter a').click(function() {
if($(this).attr('rel')) {
$('img').hide();
$('img.'+$(this).attr("rel)).show();
}
return false;
});
$('#filter a').on('click', function() {
var cat = $(this).attr('rel');
if(cat.length) $('img').hide().filter('.'+cat).show();
});
On clicking the anchor, get the category (shortened to "cat") from the anchor's rel
attribute, then if the category has length
, ie it's not empty, hide all images, then filter the images based on the class that matches the previous rel
attribute, and show those images. 单击锚点后,从锚点的rel
属性中获取类别(简称为“ cat”),然后,如果类别具有length
,即不为空,则隐藏所有图像,然后根据与上一个rel
的类过滤图像属性,并显示这些图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.