运行下面提到的代码时,出现错误消息:“ TypeError:无法读取未定义的属性'currentTarget'。”
是因为我的currentTarget提到了2个地方吗?
仅运行1个函数“ deleteFood”时,它不会给出任何错误。
<script>
let cB = document.getElementsByClassName("deleteFoodClass");
var anyBoxesUnchecked = [];
for (i = 0; i < cB.length; i++) {
cB[i].addEventListener('click', checkBoxMarked);
}
function checkBoxMarked(arg) {
var trueBoolean = "true";
let elm = arg.currentTarget;
let marked = elm.getAttribute('data-IngredientMarked');
let id = elm.getAttribute('data-ingredientID');
if(marked === true){
anyBoxesUnchecked.push(id); // ... and adding them to the array.
} else
anyBoxesUnchecked.splice(id);
}
function deleteFood(arg) {
let el = arg.currentTarget;
let id = el.getAttribute('data-ingredientID');
console.log(id); // used for debugging
var url = '/api/Ingredients/';
var urlQuery = url.concat(id);
for (i = 0; i < anyBoxesUnchecked.length; i++) {
fetch(urlQuery,
{
method: 'GET',
body: undefined,
headers: {
"Content-type": "application/json; charset=UTF-8"
}
})
//.then(res => res.json())
.catch(error => console.error('Error', error))
.then(response => console.log('Sucess On Deletion', response));
}
}
</script>
已编辑:尝试调试时会出现以下错误: Picture of debug message
解决方案:发现-由于某种原因,鉴于从未调用过checkBoxMarked-我试图用“ function()”替换它,从而解决了问题。