1
我試圖讓一個代碼塊發生取決於被點擊的元素的索引。問題是,不管我點擊哪個元素它吐出1.jQuery .index()總是返回1
$(document).ready(function() {
$('.container .product a').click(function() {
var a = $(this).index();
alert(a);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="product">
<div class="product-body">
<img src="https://placehold.it/50x50" />
<a href="#">Buy Me!</a>
</div>
</div>
<div class="product">
<div class="product-body">
<img src="https://placehold.it/50x50" />
<a href="#">Buy Me Also!</a>
</div>
</div>
<div class="product">
<div class="product-body">
<img src="https://placehold.it/50x50" />
<a href="#">M Three!</a>
</div>
</div>
</div>
感謝您參加額外的步驟,並解釋爲什麼,你的描述幫我更好地理解它。 –