私は以下のコードを書いています。ここでは、クラス todos
の新しいインスタンスを作成し、毎回別のテキストをコンストラクタに渡しています。ES6で作成された新しいインスタンスごとに別のスコープを渡す
inside setText
私はクリックメソッドを要素test
にバインドしていますので、それをクリックしてそのテキストを返します。
ここで問題となるのは、このメソッドで作成された3つのコンポーネントには別々のテキストが表示されますが、いずれの要素をクリックしても、最後のテキストは'this is a todo component3'
と表示されます。私は、それを各コンポーネントごとに分離したいと思っています。助けてください。
class todos{
constructor(text){
this.istodo = false;
this.text = text;
}
_changestatus(){
this.istodo = !this.istodo;
}
setText(){
this.getDiv = document.getElementById('test');
this.getDiv.innerHTML = this.getDiv.innerHTML+'\n'+this.text;
this.getDiv.onclick = (event)=> {alert(this.text)}; //this is always coming as "this is a todo component3"
}
}
let todo = new todos('this is a todo component');
todo.setText();
let todo1 = new todos('this is a todo component1');
todo1.setText();
let todo2 = new todos('this is a todo component2');
todo2.setText();
let todo3 = new todos('this is a todo component3');
todo3.setText();
あなたがいつも使っている 'のdocument.getElementById( 'テスト')は'右 – Holger
はい、おかげで、私はその部分を気付きませんでした。 – Shouvik