我使用回圈遍歷節點元素來從單擊的元素中獲取 ID。在那之前一切都很好,而且它有效。現在我想在我的類中呼叫另一個函式并得到這個錯誤:
this._PreLoad 未在 HTMLElement.s.onclick 中定義
this._joinnews.forEach((node) => {
node.onclick = function(){
const requestData = `newsid=${node.id}`;
// some code //
this._PreLoad(true); // ERROR
// _PreLoad(true); not working
// this._PreLoad(true).bind(this); not working (I am not yet familiar with the bind function)
}
});
節點回圈內的功能是否未知?
uj5u.com熱心網友回復:
正確的地方在bind this這里:
node.onclick = function() {
const requestData = `newsid=${node.id}`;
// some code //
this._PreLoad(true);
}.bind(this); // bind here!
或者,使用一個箭頭函式,它保留this函式宣告時的任何內容:
node.onclick = () => {
const requestData = `newsid=${node.id}`;
// some code //
this._PreLoad(true);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/418668.html
標籤:
上一篇:為什么在復選框事件偵聽器上使用“this”不會回傳dom物件,而是回傳window物件?[復制]
下一篇:React:動態添加輸入文本
