jQuery:向元素添加标题属性并从以下元素插入文本

标签 jquery image attributes title

我什至不确定这是否可行,但让我们尝试一下。

我为每个图像动态生成了这个 HTML:

<a href="image.jpg" class="thickbox">
    <img src="thumb.jpg" title="This is the title" alt="" />
</a>

不知何故,我需要从thumb.jpg中获取标题属性并将其复制到周围的 anchor ,这将给出以下结果:

<a href="image.jpg" class="thickbox" title="This is the title" alt="">
    <img src="thumb.jpg" title="This is the title" alt="" />
</a>

如果这可以通过 jQuery 实现,我会很高兴。

提前致谢。

最佳答案

$('a.thickbox img').each(function(key,$elem){
    $(this).parent().attr('title',$(this).attr('title'));
});

如果您有多个图像,则循环使用每个图像。否则,请使用函数中的行并将 $(this) 替换为 $('a.thickbox img')

关于jQuery:向元素添加标题属性并从以下元素插入文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12560032/

相关文章:

html - CSS img 对齐 : how to set a img middle-right align in a DIV

html - 使用属性选择器在 CSS 伪元素中换行

Python -- Pygame 属性错误 : int object has no attribute 'draw'

javascript - 在动态函数中覆盖 jQuery 全局对象变量

jquery - 在输入框光标后添加不可编辑的文字

php - 使用 jquery、php、mysql 动态弹出

java - 这个错误出现在我的java程序中,似乎没有任何原因

javascript - 每次点击将类添加到单个 div

java - 使用 JavaFX 使 ImageView 居中

powershell - 如何在 V2 中模拟 PowerShell V3 属性