我的代码有问题,它是一个货币计算器,你可以存储你的结果,它工作正常,但我也想让它能删除结果,函数removeRow应该这样做,但当我试图使用我有一个控制台错误,如:
Uncaught ReferenceError: removeRow is not defined
at HTMLInputElement.onclick (index.html:1)
我已经尝试过我找到的所有内容,没有任何帮助
这是我的代码:
document.addEventListener("DOMContentLoaded", function(event) {
console.log("DOM is ready");
const operationConfirm = document.getElementById('operationConfirm');
const resultList = document.getElementById('tableBody');
const operation = {
euroValue: document.getElementById('euroValue'),
operationName: document.getElementById('operationName'),
operationValue: document.getElementById('operationValue')
}
operation.euroValue.onchange = updateValues;
operationConfirm.addEventListener("click", function() {
let thisOperationValue = operation.operationValue.value;
let thisOperationName = operation.operationName.value;
let thisEuroValue = parseFloat(operation.euroValue.value);
let calcResoult = calcCurrency(thisEuroValue, thisOperationValue);
let newOperation = document.createElement('tr');
newOperation.innerHTML = `
<td>${thisOperationName}</td>
<td class="amountToChange">${thisOperationValue}</td>
<td class="resultValue">${calcResoult.toFixed(2)} PLN</td>
<input type="button" value="X" onclick="removeRow(this)" id="deleteBtn"/>`;
const btns = resultList.getElementsByTagName('input');
const delBtns = Array.from(btns);
for( let i = 0; i < delBtns.length; i++) {
delBtns[i].setAttribute('click', 'removeRow(this)');
}
resultList.appendChild(newOperation);
});
function calcCurrency(eValue, quantity) {
return quantity * eValue;
}
function updateValues() {
const outComePLN = Array.from( document.getElementsByClassName('resultValue'));
const outComeEur = Array.from(document.getElementsByClassName('amountToChange'));
for(let i = 0; i < outComePLN.length; i++) {
outComePLN[i].innerHTML = (outComeEur[i].innerHTML * operation.euroValue.value).toFixed(2);
}
};
function removeRow(btn) {
const thisTable = document.getElementById('tableBody');
thisTable.deleteRow(btn.parentNode.parentNode.rowIndex);
}
});
和HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Klkulator walut</title>
</head>
<body>
<style>
#cantor{
margin: 10vh auto 0 auto;
border: 1px solid black;
width: 50%;
min-height: 300px;
}
</style>
<div id="cantor">
<p>1 € = <input type="text" value="4.14" id="euroValue">PLN</p>
<input type="text" placeholder="Nazwa operacji" id="operationName">
<input type="text" placeholder="ilość €" id="operationValue">
<input type="submit" placeholder="Oblicz" id="operationConfirm">
<table id="results">
<thead>
<tr>
<th>Nazwa operacji</th>
<th>Kwota €</th>
<th>Kwota PLN</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
<script type="text/javascript" src="main.js"></script>
</div>
</body>
</html>
这是codepen:
答案 0 :(得分:1)
只需将function removeRow(btn)
- 定义移出EventListener-Definition即可。在该范围内创建函数时,在该函数外部将不会显示该函数。
document.addEventListener("DOMContentLoaded", function(event) {
[...]
});
function removeRow(btn) {
const thisTable = document.getElementById('tableBody');
thisTable.deleteRow(btn.parentNode.rowIndex-1);
}
答案 1 :(得分:1)
只需在removeRow
之外声明addEventLister
方法即可。它有效。
单击该按钮,它将在全局范围内查找该功能。所以你需要全局声明。