【问题标题】:jQuery - select groups of child elements based on the parent DIV elementjQuery - 根据父 DIV 元素选择子元素组
【发布时间】:2011-03-21 17:23:40
【问题描述】:

我有一个普通的 html 页面,其中包含 div 和图片链接等内容:

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

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

...

我正在尝试在所有以 jpg/gif/png 扩展名结尾的链接上挂接灯箱脚本。

现在,根据我之前提出的问题:),我有:

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

gallery内的所有链接分组。

但我想将每个 div 的链接分组到他们自己的画廊中。例如,gallery1 中的 .foo' 和 .boo 链接以及 gallery2 中的 .bah 和 .gah 等等...

我该怎么做?

【问题讨论】:

    标签: javascript jquery jquery-selectors lightbox


    【解决方案1】:
    $('div').each(function(index) {
       $(this).find('a').filter(function(){
           return this.href.match('[\.jpg|\.png|\.gif]$');
       }).colorbox({
           rel: 'gallery' + index
       });
    });
    

    【讨论】:

      【解决方案2】:

      (抱歉,无法评论其他答案)

      您的正则表达式需要括号,而不是括号:

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

      否则,您实际上是在匹配字符 .jpgpnif|foo| 匹配的文件名。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多