我将一个变量保存到一个数组中,点击一个项目,但每次点击另一个项目时都会改变。目前,这是警告相同的值,而不是新值。
<div class="item">
<span id="0" class="video" data-vimeoid="http://vimeo.com/XXX"></span>
</div>
<div class="item">
<span id="1" class="video" data-vimeoid="http://vimeo.com/SSS"></span>
</div>
<div class="item">
<span id="2" class="video" data-vimeoid="http://vimeo.com/ZZZ"></span>
</div>
然后在jQuery中
var previous = [];
$(document).on( 'click', '.item:not(.is-expanded)', function() {
var url = videoSpan.data("vimeoid");
previous.push(url);
alert(previous[0]);
});
答案 0 :(得分:2)
你应该做
var previous = [];
$(document).on( 'click', '.item:not(.is-expanded)', function() {
var url = videoSpan.data("vimeoid");
previous.push(url);
alert(previous[previous.length-1]);
});