获取"alt"来自位于< a>内部的图像的属性标签 [英] Get the "alt" attribute from an image that resides inside an <a> tag

查看:100
本文介绍了获取"alt"来自位于< a>内部的图像的属性标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该很简单,但是由于某些原因,它是行不通的.

this should be fairly simple but for some reason, it doesn't work.

我想从标签内的图片中获取"alt"属性

I want to get the "alt" attribute from an image that resides inside an tag

<div id="album-artwork">
<ul>
   <li><a href="link to large image"><img src="thumbnail" alt="description of the image" /></a></li>
   ...
</ul>
</div>



$("#album-artwork a").click(function(e) {
e.preventDefault();

var src = $(this).attr("href");
var alt = $(this).next("img").attr("alt");


alert(src); // ok!
alert(alt); //output: undefined

});

有什么想法为什么next()不起作用?有更好的解决方案吗?

any ideas why next() doesn't work? Is there a better solution out there?

先谢谢了 马可

推荐答案

这是因为image标签位于ul标签内.下一个仅查找相同级别的标签.您必须使用

It's because the image tag is inside the ul tag. next only looks for tags at the same level. You'll have to use

var alt = $(this).children("img").attr("alt");

包括缺少的其他"

这篇关于获取"alt"来自位于&lt; a&gt;内部的图像的属性标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆