爲什麼我需要點擊兩次才能在'重置'後更改類?我該如何解決這個問題?Javascript'reset'函數無法正常工作
期望的結果是將函數恢復到初始狀態並正常循環數組。下面的演示。
$(function() {
var weights = ["jHairline", "jThin", "jLight", "jMedium"];
var currentIndex = 0;
$('#text').on('click', function (e) {
$("h1").removeClass().addClass(weights[currentIndex]);
$("h1").html(weights[currentIndex]);
if (currentIndex == weights.length - 1)
currentIndex = 0;
else
currentIndex++;
e.preventDefault();
});
$('#reset').click(function() {
currentIndex = 0;
$("h1").removeClass().addClass(weights[currentIndex]);
$("h1").html(weights[currentIndex]);
});
});
初始狀態是指「重量」還是「jHairline」? – CapitanFindus