JS-History-Recoder 3

by homura0731

HTML

<button id="prev3" type="button">上一步</button>
<button id="next3" type="button">下一步</button>
<br><br>
<textarea id="text3" cols="5" style="width:300px"></textarea>

JavaScript

var step3 = 0; // 步驟變數
var textList3 = new Array(3); // array容器
var prev3 = document.getElementById('prev3');
var next3 = document.getElementById('next3');
var text3 = document.getElementById('text3');
function textWrite3(){
    // 判斷是否已到最大紀錄步驟
    if(step3 === textList3.length){
        var temp;
        // 資料向前移動
        for(var i = 0;i < textList3.length - 1; i++){
            textList3[i] = textList3[i + 1];
        }
        textList3[step3 - 1] = text3.value; 
    } else {
        textList3[step3] = text3.value;
        step3++; 
    }
}
text3.addEventListener('input', textWrite3);
prev3.onclick = function(){
    if(step3 < 2){
        alert('沒有上一步');
    }else{
        step3--;
        text3.value = textList3[step3 - 1];
    }
}
next3.onclick = function(){
    if(step3 > textList3.length - 1){
        alert('沒有下一步');
    }else{
        step3++;
        text3.value = textList3[step3 - 1];
    }
}