3 回答
TA贡献1900条经验 获得超5个赞
你的意思是这样的:
var indexLast = '';
$('.item').click(function() {
var index = $('.item').index($(this));
$('.result').text( 'current: ' + index + ', previous: ' + indexLast);
indexLast = index
});
.wrap {
display: flex;`enter code here`
}
.item {
background-color: #2ecc71;
width: 50px;
height: 50px;
margin: 0 10px;
border-radius: 50%;
cursor: pointer;
}
.item:nth-child(even) {
background-color: #3498db;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="result"></div>
TA贡献1893条经验 获得超10个赞
作为存储在变量中的替代方法,您可以将一个类添加到您单击的那个类,然后在您下次单击时检查该类。
$(".wrap>.item").click(function() {
var last = $(this).siblings(".active").removeClass("active");
$(this).addClass("active");
// output last.text() / $(this).text() as needed
});
使用问题中指定的 OPs 按钮和 this.last index() :
$('.item').click(function() {
var indexLast = $(".item.clicked").index();
var index = $(this).index();
$(this).addClass("clicked").siblings().removeClass("clicked");
$('.result').text(index + ', ' + indexLast);
});
.wrap {
display: flex;
}
.item {
background-color: #2ecc71;
width: 50px;
height: 50px;
margin: 0 10px;
border-radius: 50%;
cursor: pointer;
}
.item:nth-child(even) {
background-color: #3498db;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="result"></div>
使用 css 扩展它以显示 active/lastactive 给出:
$(function() {
$(".wrap>.item").click(function() {
var last = $(this)
.siblings()
.removeClass("lastactive")
.filter(".active")
.removeClass("active")
.addClass("lastactive");
$(this).addClass("active");
});
});
.item {
border: 1px solid black;
height: 20px;
}
.active {
background-color: yellow;
}
.lastactive {
background-color: #CCC;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="result"></div>
TA贡献1866条经验 获得超5个赞
'use strict';
let indexLast = 'here prev index', current_i = 'here prev index'
$('.item').click(function() {
let index = $('.item').index(this),
last_i = current_i;
current_i = index;
$('.result').text(current_i + ', ' + last_i);
});
.wrap {
display: flex;
}
.item {
background-color: #2ecc71;
width: 50px;
height: 50px;
margin: 0 10px;
border-radius: 50%;
cursor: pointer;
}
.item:nth-child(even) {
background-color: #3498db;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="result"></div>
添加回答
举报