我有這個的jsfiddle設置了我的問題:http://jsfiddle.net/7MjN6/使用`this`在一個循環的目標當前項目
正如你所看到的,我有兩個圖像。我想每個單擊的周圍的div都展開。我試圖使用this
對象來確保單擊圖像只會擴展該圖像的div,但我確定我使用的是this
錯誤,因爲它沒有連接。如果有人可以刺我的小提琴將非常感激!
HTML:
<body>
<div class="img-container">
<a href="#"><img src="http://blueantlabs.com/wp-content/themes/blueantplate/img/port-wide/thefutureproject-605.png" /></a>
</div>
<div class="img-container">
<a href="#"><img src="http://blueantlabs.com/wp-content/themes/blueantplate/img/port-wide/thefutureproject-605.png" /></a>
</div>
</body>
JS:
$(function(){
$('.img-container').each(function(index){
var imgHeight = $(" a img").height();
$('a').click(function(e){
e.preventDefault;
$('div').animate({height: imgHeight});
});
});
});
你不使用'this'可言。我甚至搜索過你的代碼,零次出現的單詞「this」 – MrOBrian
這是我的錯誤,沒有鏈接到最新的小提琴。 – alexvance