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

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

xButton.setAttribute 不是函數錯誤

xButton.setAttribute 不是函數錯誤

慕婉清6462132 2021-06-03 17:16:35
我有一個函數可以將 JSON 文件中的元素格式化為表格。該表工作正常,但現在我試圖在另一列上實現一個按鈕,該按鈕從文件中刪除所述元素。但是當我這樣做時,它出現了錯誤: Uncaught (in promise) TypeError: xButton.setAttribute is not a function這是我的代碼:      async function table() {        let response = await fetch('/tasks', {          method: "GET",          headers: {          'Content-Type': 'application/json'          }        });        let jsonPayload = await response.json();        var table = document.getElementById("tableBody");        tableBody.innerHTML = "";        for(var i = jsonPayload.length - 1; i > -1; i--) {          var row = tableBody.insertRow(0);            var firstColumn = row.insertCell(0);            var secondColumn = row.insertCell(1);            var thirdColumn = row.insertCell(2);            var fourthColumn = row.insertCell(3);            var xButton = '<button>x</button>';            xButton.setAttribute("onclick", `deleteElement('${jsonPayload[i].id}')`);          firstColumn.innerHTML = jsonPayload[i].id;          secondColumn.innerHTML = jsonPayload[i].desc;                  thirdColumn.innerHTML = jsonPayload[i].importance;          fourthColumn.innerHTML = xButton;        }              }這是刪除元素的代碼//function which deletes a task.       function deleteElement() {      //Variable id is the number submitted in the input form to Delete tasks        var id = document.getElementById("deleteId").value;      //This is the url that is fetched with the variable id which deletes the function using the code from the backend.       var url = "/tasks/"+id;      //isNan checks whether id is not a number      if (isNaN(id)) {        //If it is not a number the error message is displayed.         alert("The value inputted must be a number!");        //Returns out of the function without proceeding        return;      }主要問題是 xButton,任何幫助表示贊賞。
查看完整描述

3 回答

?
慕妹3242003

TA貢獻1824條經驗 獲得超6個贊

var xButton = document.createElement("BUTTON");

xButton.innerHTML = "x";

xButton.setAttribute("onclick", `deleteElement('${jsonPayload[i].id}')`);


// then at the end, instead of "fourthColumn.innerHTML = xButton;" you should do this

fourthColumn.appendChild(xButton);


查看完整回答
反對 回復 2021-06-11
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

您需要使用createElement來創建一個按鈕,因為目前它只是一個字符串..


var xButton = document.createElement("button");

xButton.innerHTML = "x";

xButton.setAttribute("onclick", `deleteElement('${jsonPayload[i].id}')`);


查看完整回答
反對 回復 2021-06-11
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

您需要使用正確的代碼創建它。另外我建議你試試JQuery。


var button = document.createElement("button");

button.innerHTML = "Do Something";


var body = document.getElementsByTagName("body")[0];

body.appendChild(button);


button.addEventListener ("click", function() {

  alert("test");

});


查看完整回答
反對 回復 2021-06-11
  • 3 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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