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

javascript - jQuery - select groups of child elements based on the parent DIV element - Stack Overflow

programmeradmin4浏览0评论

I have a normal html page with stuff like divs and links with images:

<div>
  <a href="foo.jpg"> ... </a>
  <a href="boo.jpg"> ... </a>
</div>

<div>
  <a href="bah.jpg"> ... </a>
  <a href="gah.jpg"> ... </a>
</div>

...

I'm trying to hook a lightbox script on all links that end with jpg/gif/png extensions.

Right now, based on a previous question I asked :), I have:

 $('div a').filter(function(){
   return this.href.match('[\.jpg|\.png|\.gif]$');
 }).colorbox({
   rel: 'gallery'
 });

which groups all links inside gallery.

But I would want to group links from each div inside their own gallery. For example .foo' & .boo links inside a gallery1 and .bah & .gah inside gallery2 and so on...

How can I do that?

I have a normal html page with stuff like divs and links with images:

<div>
  <a href="foo.jpg"> ... </a>
  <a href="boo.jpg"> ... </a>
</div>

<div>
  <a href="bah.jpg"> ... </a>
  <a href="gah.jpg"> ... </a>
</div>

...

I'm trying to hook a lightbox script on all links that end with jpg/gif/png extensions.

Right now, based on a previous question I asked :), I have:

 $('div a').filter(function(){
   return this.href.match('[\.jpg|\.png|\.gif]$');
 }).colorbox({
   rel: 'gallery'
 });

which groups all links inside gallery.

But I would want to group links from each div inside their own gallery. For example .foo' & .boo links inside a gallery1 and .bah & .gah inside gallery2 and so on...

How can I do that?

Share Improve this question asked Mar 21, 2011 at 17:23 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8
$('div').each(function(index) {
   $(this).find('a').filter(function(){
       return this.href.match('[\.jpg|\.png|\.gif]$');
   }).colorbox({
       rel: 'gallery' + index
   });
});

(sorry, can't ment on the other answer)

Your regex needs parentheses, not brackets:

(\.jpg|\.png|\.gif)$

otherwise you're literally matching the characters .jpgpnif| and a filename of foo| matches.

发布评论

评论列表(0)

  1. 暂无评论