有沒有辦法在方法createElement()內部呼叫方法?我無法在方法內訪問函式內的方法。checkbox.off().on('click', function () {})checkBoxes()
class Lists {
constructor() {
this.currentList = [];
this.completedList = [];
}
createElement(
value1 = undefined,
value2 = undefined,
value3 = undefined,
checkboxCheck = ''
) {
value3.append(`<li >
<input type="checkbox" ${checkboxCheck}/>
<span >
${value1}
</span>
<div >
<i ></i>
<i ></i>
</div>
<br />
<span >${value2}</span>
</li>`);
}
checkBoxes() {
const checkbox = $('input[type=checkbox]');
checkbox.off().on('click', function () {
this.createElement(value1, value2...)
});
}
}
uj5u.com熱心網友回復:
有3種方式。
// set 'this' to 'self' outside the function
const self = this;
checkbox.off().on('click', function () {
self.createElement(value1, value2...)
});
//use Function.prototype.bind to pass 'this'
checkbox.off().on('click', function () {
this.createElement(value1, value2...)
}.bind(this));
//use arrow function
checkbox.off().on('click', () => {
this.createElement(value1, value2...)
});
但我建議您通過搜索找出為什么“this”在您的代碼中不起作用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/477546.html
標籤:javascript jQuery 班级 哎呀 方法
上一篇:如何從動態選項卡中獲取陣列資料并通過jquery洗掉當前選項卡
下一篇:使用按鈕更改div內容
