亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

(JS) 拼接替換數組中的值,只替換第一個值

(JS) 拼接替換數組中的值,只替換第一個值

絕地無雙 2023-05-19 17:10:14
我有一個網頁,我試圖在其中選擇和替換/刪除一個值。當我單擊選擇時,在控制臺中它顯示我正在選擇我想要選擇的項目,但是當將值輸入到 splice 命令時它只會更改數組的第一個條目。第一個函數是根據位置選擇柱,section 函數是柱數組值的位置,然后出現一個柱和按鈕,如果值為 -1,它將刪除該值,如果不是,它將替換它    function clickBar(Event) {    //Receives position in xy for user clicks    var posX = Event.clientX;    var posY = Event.clientY;    console.log("X = "+posX +" Y = "+ posY);    var barWidth= 500/barVals.length;    console.log("Bar Width = "+barWidth);    var barNum;    //checks to see if click was within the confines of the area that the boxes are displayed    //if so barNum is calculated to show which position in the array you are clicking on    if(posY >topY && posY < bottomY && posX > leftX && posX < rightX){        console.log("Inside");        barNum = Math.floor((posX - leftX) / barWidth);        console.log("Bar Number = "+barNum);    } else {        console.log("Outside");}    if (barNum > -1) {        replaceBar(barNum)    }    console.log(barVals);    draw();}document.addEventListener("click", clickBar);function replaceBar(barNum) {    console.log("Bar Number2 = "+barNum);    var replaceBox = document.getElementById('replaceVal');    var replaceBtn = document.getElementById('replaceBtn');    var displayBoxSetting = replaceBox.style.display;    var displayBtnSetting = replaceBtn.style.display;    //hiding and displaying the edit text box and button    if (displayBoxSetting == 'block' && displayBtnSetting=='block') {        replaceBox.style.display = 'none';        replaceBtn.style.display = 'none';    }    else {        replaceBox.style.display = 'block';        replaceBtn.style.display = 'block';    }問題是:if(replaceBox.value >0) {    barVals.splice(barNum, 1, parseInt(replaceBox.value));它應該從長度為 1 的位置 barNum 開始,并獲取替換框的 int 值并使用它來拼接該值編輯 - 我在頁面的替換屏幕截圖之前和第二個屏幕截圖之后包含了頁面圖像
查看完整描述

1 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

好吧,看來我是以完全錯誤的方式開始的,所以這里有一個足夠不同的解決方案。這是放在繪制函數中,取自畫布本身。


canvas.onclick = function mousePos(Event) {



    var rect = canvas.getBoundingClientRect();

    var posX = Event.clientX- rect.left;

    var posY = Event.clientY - rect.height;

    console.log("X = "+posX +" Y = "+ posY);


    var barWidth= 500/barVals.length;


    console.log("Bar Width = "+barWidth);


    var barNum;


    barNum = Math.floor((posX-60) / barWidth);


if(barNum>=0 && barNum < barVals.length) {

    var position = barNum;

    console.log(position);

    var amount = prompt("Please enter the new value", "734");

    //barVals[position] = amount;


    if(amount >0) {

        barVals.splice(position, 1, parseInt(amount));

        console.log(amount);

        draw(); // redraw


    }

    else if(amount = -1){

        barVals.splice(position, 1);

        colours.splice(position, 1)

        draw(); // redraw

    }else{

        alert("Incorrect value entered");

    }

}


}


查看完整回答
反對 回復 2023-05-19
  • 1 回答
  • 0 關注
  • 276 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號