2012-03-23 9 views
1

子ノードのalt属性が見つかりません。私は正しい親を持っています。私は間違って何をしていますか?イメージの子ノードjqueryを見つけることができません

Bar1は良いですが、私はbar2を見つけることができません。

<ul class="small-image"> 
    <li><a id="foo1"><img src="../images/imageSlide/s2.jpg" alt="pic1"></a></li> 
    <li><a id="foo2"><img src="../images/imageSlide/s2.jpg" alt="pic2"></a></li> 
<ul> 
$('ul.small-image li a').bind('click',function(){ 
    var elm = '#'+$(this+'div:first-child').attr('alt'); 
    //var elm = '#'+$(this+'img').attr('alt'); 
    console.log("Bar1: "+$(this).attr('id')) ; 
    console.log("Bar2: "+elm); 

}); 
+2

jsfiddle:何のdivありません最初の-child'を? – m90

+0

これは無効です: '#' + $(this + 'div:first-child')。attr( 'alt');とにかくバー2とは何ですか?あなたは何を得ようとしていますか? alt属性? –

答えて

0

子ノードの属性は最初の子(のみfoo1の)を返します。私はあなたがjQueryの.children()機能をチェックアウトしたいと思うかもしれないと思う。

1

は、私はあなたがAになる画像

$('ul.small-image li a').bind('click',function(){ 
    console.log("Bar1: "+ $(this).attr('id')) ; 
    console.log("Bar2: "+ $(this).find("img").attr("alt")); // Find the image INSIDE the current A 

});​ 

thisからAltキーを選択しようとしていると思うので、あなたがクリックされた内部の画像を検索し、属性altを取得する必要があります。ただ、$(this).find("img").attr("alt")

とを使用し、なぜあなたは `div要素を選択しようとしているhttp://jsfiddle.net/kL5P5/

+0

ありがとう..... find()はチケットです。 –

+0

答えが=の場合は、[OK] –

関連する問題