获取“替代”来自驻留在 内的图像的属性标签

发布于 2024-09-26 05:02:22 字数 544 浏览 3 评论 0原文

这应该相当简单,但由于某种原因,它不起作用。

我想从标签内的图像中获取“alt”属性,

<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() 不起作用?有更好的解决方案吗?

提前致谢 马可

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

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

});

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

Thanks in advance
Marco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

最近可好 2024-10-03 05:02:23

尝试使用 $(this).find('img') 来选择链接内的某个元素。

Try $(this).find('img') instead to select some element within the link.

七秒鱼° 2024-10-03 05:02:23

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

测试:http://jsbin.com/amado4

or

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

Test : http://jsbin.com/amado4

静谧幽蓝 2024-10-03 05:02:23

没关系。我刚刚明白了...我意识到 img 不是 的兄弟,而是 的子级代码>

这是正确的语法

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

Nevermind. I just got it... I've realized that the img is not a sibling of <a> but a child of <a>

This is the correct syntax

var alt = $(this).children("img").attr("alt"); 
可爱咩 2024-10-03 05:02:23

.next 不会选取下一个元素(即下一个 Li 而不是 IMG)吗?

Won't .next pick up the next element which is the next Li and not the IMG?

夢归不見 2024-10-03 05:02:23

尝试一下这个测试,它的工作原理!!!

<div id="album-artwork">
<ul>
   <li><a href="javascript:void(0);"><img src="images/ajax-loader.gif" alt="ajax-loader" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/close.png" alt="close" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/copy.png" alt="copy" /></a></li>
</ul>
</div>

jQuery 代码是

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

            var src = $(this).attr("href");
            //var alt = $(this).next("img").attr("alt");
            var alt = $(this).children().attr("alt");
            //find function also do the same thing if you want to use.
            /*var alt = $(this).find("img").attr("alt");*/

            alert(src); // ok!
            console.log(src); // ok!
            alert(alt); //output: not undefined now its ok!
            console.log(alt); //output: not undefined now its ok!
        });
    });

children() 函数查找任何子项,但如果您想查找任何特定子项,请定义子项名称,如下所示 children("img") 孩子(“按钮”)

try this tested and its working!!!

<div id="album-artwork">
<ul>
   <li><a href="javascript:void(0);"><img src="images/ajax-loader.gif" alt="ajax-loader" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/close.png" alt="close" /></a></li>
   <li><a href="javascript:void(0);"><img src="images/copy.png" alt="copy" /></a></li>
</ul>
</div>

and jQuery code is

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

            var src = $(this).attr("href");
            //var alt = $(this).next("img").attr("alt");
            var alt = $(this).children().attr("alt");
            //find function also do the same thing if you want to use.
            /*var alt = $(this).find("img").attr("alt");*/

            alert(src); // ok!
            console.log(src); // ok!
            alert(alt); //output: not undefined now its ok!
            console.log(alt); //output: not undefined now its ok!
        });
    });

the children() function find any children but if you want to find any specific children define the children name like this children("img") OR children("button").

静谧幽蓝 2024-10-03 05:02:23

不要复杂化:

$('img').on({'click': function(){
    var alt = this.alt;         

Dont complicate:

$('img').on({'click': function(){
    var alt = this.alt;         
风启觞 2024-10-03 05:02:22

这是因为图像标签位于 ul 标签内。 next 只查找同一级别的标签。您必须使用

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

编辑:包括缺少的附加“

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");

EDIT: included the additional " that was missing

写给空气的情书 2024-10-03 05:02:22
$(this).find('img').attr('alt'); 
$(this).find('img').attr('alt'); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文