【问题标题】:Get text from <tr> and add to img alt in another <tr>从 <tr> 获取文本并添加到另一个 <tr> 中的 img alt
【发布时间】:2013-07-22 03:24:46
【问题描述】:

我想从博主 blogspot 标题表中复制文本并将其添加到 img alt,以便它将作为标题/标题出现在 fancybox 幻灯片中。

所以,这是我拥有的 HTML:

<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: right; text-align: right;"><tbody>
<tr>           <td style="text-align: center;"><a href="URL"><img align="right" alt="" src="URL" title="Ghostly Waves in the Valle de Rocas" width="320" /></a></td>         </tr>
<tr>           <td class="tr-caption" style="text-align: center;">This is my Caption</td>         </tr>
</tbody></table>

我的脚本是:

   $("tr-caption").each(function(){
     var Caption = $(this).text();

     if (Caption && Caption !== ''){
         $(this).parent(img).attr('alt', Caption );
     }
});

这是我希望的输出......

<table cellpadding="0" cellspacing="0" class="tr-caption-container" style="float: right; text-align: right;"><tbody>
<tr>           <td style="text-align: center;"><a href="URL"><img align="right" alt="This is my Caption" src="URL" title="Ghostly Waves in the Valle de Rocas" width="320" /></a></td>         </tr>
<tr>           <td class="tr-caption" style="text-align: center;">This is my Caption</td>         </tr>
</tbody></table>

但它不起作用......

这是一个 jsfiddle:http://jsfiddle.net/NCqW2/6/

非常感谢任何帮助!感谢您花时间查看!

【问题讨论】:

    标签: javascript jquery fancybox


    【解决方案1】:

    更换

    $(this).parent(img).attr('alt', Caption );
    

    $(this).parent().prev().find('img').attr('alt', Caption );
    

    应该这样做。

    【讨论】:

    • 或者您可以使用techfoobar.com/jquery-next-in-dom 并直接使用$(this).prevInDOM('img').attr('alt', Caption ); - 免责声明:我写的。在这里发布它纯粹是因为它完全适合用例。
    • 这真是太快了!!....太快了,我还不能将其标记为正确!谢谢大家,真的是很大的帮助,无疑为我节省了大量时间!
    猜你喜欢
    • 2016-12-26
    • 2018-01-05
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-04
    • 2017-05-06
    相关资源
    最近更新 更多