14
4

8 に答える 8

26

imageタグがulタグの中にあるからです。next は、同じレベルのタグのみを検索します。使用する必要があります

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

編集:欠落していた追加の「」が含まれていました

于 2010-10-07T16:50:20.207 に答える
8
$(this).find('img').attr('alt'); 
于 2012-05-04T10:56:22.593 に答える
3

また

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

テスト: http://jsbin.com/amado4

于 2010-10-07T17:00:33.527 に答える
3

$(this).find('img')代わりに、リンクの要素を選択してみてください。

于 2010-10-07T16:50:29.960 に答える
2

どうでも。imgわかった...は の兄弟では<a>なく の子であることに気付きました<a>

これは正しい構文です

var alt = $(this).children("img").attr("alt"); 
于 2010-10-07T16:51:52.843 に答える
1

これをテストして試してみてください!!!

<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") OR children("button")のように子の名前を定義します。

于 2014-02-07T05:13:54.050 に答える
1

.next は、IMG ではなく次の Li である次の要素を取得しませんか?

于 2010-10-07T16:51:29.437 に答える