我如何创建一组按钮,当单击时删除存储在关联数组中的各个元素?我已经尝试了拼接元素..但它似乎不起作用。我是编程的新手,所以任何帮助都会受到赞赏。谢谢
bodyText = bodyText + '<input type="button" id="btnDeleteQuestion"
value="Delete a question" onClick="questionBank.splice.(0,1) ">';
JavaScript中的关联数组是具有键/值对的普通旧对象。使用delete
运算符删除密钥。
var myObject = {
key1:"key1Value",
key2:"key2Value",
key3:"key3Value",
key4:"key4Value"
};
console.log(myObject);
document.getElementById("btnDeleteQuestion").addEventListener("click", function(){
delete myObject["key4"];
console.log(myObject);
});
<input type="button" id="btnDeleteQuestion" value="Delete a question">