一定有一些簡單的我很想念。我試圖獲取元素的索引,但保持-1。問題與jQuery索引()
HTML:
<div id="rating_boxes">
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
<img src="/img/ratingbox.gif" class="ratingbox" alt="Rate this Speech" />
</div>
的jQuery:
$("img.ratingbox").hover(function() {
var index = $(this).parent().index(this);
// have also tried $("#rating_boxes").index(this);
// and $("#rating_boxes").index($(this));
// and $(this).parent().index($(this));
alert(index);
$(this).attr('src', '/img/ratingbox-selected.gif');
}, function() {
$(this).attr('src', '/img/ratingbox.gif');
});
如果可以的話,我會贊成這20次。 .index()沒有意義。 – nipponese 2013-07-30 21:48:56
偉大的方法! – Alexander 2016-04-22 13:55:54