代码之家  ›  专栏  ›  技术社区  ›  matt

jQuery每次迭代:每个库元素都应该有标题!

  •  1
  • matt  · 技术社区  · 14 年前

    简单又烦人的问题,但我需要答案相当快,我找不到解决办法。

    $('.gallery-item .gallery-icon a').each(function(){
            $('.gallery-caption').prepend("<span class='imgtitle'>"+$(this).attr("title")+"</span>")
        });
    

    每个图库项目下面都有图库标题。我反复浏览每个图库图标并找到链接的标题。我想在每个图库项目标题前加上标题。

    现在,所有的图片标题都被预先准备好了。我只想在当前图像的标题前加上前缀。

    2 回复  |  直到 14 年前
        1
  •  1
  •   Andrew67    14 年前

    你在所有的字幕上都加了标题,因为:

    $('.gallery-caption').prepend(...
    

    编辑:假设链接是标题的同级链接,类似的操作应该可以:

    $(this).parent().children('.gallery-caption').prepend(...
    
        2
  •  0
  •   dmitko    14 年前

    $('.gallery-item .gallery-icon a').each(function(){
            $(this).parent().prepend("<span class='imgtitle'>"+$(this).attr("title")+"</span>")
        });
    

    或其他遍历方法。