我有一個簡單的類。
我遇到的問題是我的 onClick 函式在單擊 div 元素時從未被呼叫。
class card {
constructor(parent, element) {
this.parent = parent;
this.element = element;
this.element.on({
'click': $.proxy(this.onClick, this)
});
}
onClick() {
console.log("onclick");
}
}
class cards {
constructor() {
this.element = $(".cards");
this.cards = [];
this.element.children(".card").each((obj) => {
this.cards.push(new card(this, $(obj)));
});
}
}
var my_a = new cards();
uj5u.com熱心網友回復:
each(index, element)將兩個引數傳遞給回呼。第一個是索引,第二個是元素。您正在包裝索引,而不是 dom 元素。
每個檔案
class card {
constructor(parent, element) {
this.parent = parent;
this.element = element;
this.element.on({
'click': $.proxy(this.onClick, this)
});
}
onClick() {
console.log("onclick");
}
}
class cards {
constructor() {
this.element = $(".cards");
this.cards = [];
this.element.children(".card").each((obj, el) => {
console.log(el);
this.cards.push(new card(this, $(el)));
});
}
}
var my_a = new cards();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<div class="cards">
one
asdf
<div class="card">
card 1
asdf
</div>
<div class="card">
card 2
asdf
</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/377285.html
標籤:javascript 查询
