【问题标题】:jQuery find() to find the <figcaption> text that follows <a>jQuery find() 查找 <a> 后面的 <figcaption> 文本
【发布时间】:2012-09-15 12:11:45
【问题描述】:

我找不到找到 的文本并将其插入到灯箱链接的标题属性中的方法。下面的代码返回第一个标题的文本,无论我点击什么链接:

<script>
$(document).ready(function () {
$('a[rel^=lightbox]').attr('title', $(this).find('figcaption').html());

        });
</script>

<body>
<a href="images/image-7.jpg" rel="lightbox[9-10-12]"><figure>
        <img src="images/thumbs/image-7_thumb.jpg" alt="First image" >
        <figcaption>Image 1</figcaption>
    </figure></a>
<br/>
<a href="images/image-8.jpg" rel="lightbox[9-10-12]"><figure>
        <img src="images/thumbs/image-8_thumb.jpg"/>
        <figcaption>Thing 2</figcaption>
    </figure></a>
</body>

【问题讨论】:

    标签: jquery lightbox caption


    【解决方案1】:

    您需要这样做:

    $('a[rel^=lightbox]').each(function() {
      $(this).attr('title', $(this).find('figcaption').text();
    });
    

    【讨论】:

      【解决方案2】:

      你的点击事件在哪里??

      <script>
         $(document).ready(function () {
         $('a').click(function(){
         $('a[rel^=lightbox]').attr('title', $(this).find('figcaption').html());
         })
           });
      </script>
      

      【讨论】:

        【解决方案3】:

        这应该可行:

          $('a[rel^=lightbox]').attr('title', $(this).parent().find('figcaption').text();
        

        需要在a元素的父元素中找到figcaption元素。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-16
          • 1970-01-01
          • 1970-01-01
          • 2012-08-06
          • 2017-12-18
          • 2018-05-04
          相关资源
          最近更新 更多