你期望这个模板...
this.div = `
<div class="a-die d-flex align-items-center justify-content-center mx-4">
<h1 class="die-text">${this.value}</h1>
</div>`;
... '活',每当this.value 的值发生变化时都会刷新自己。但这就是 HTML 的工作原理——这正是近年来所有这些数据绑定框架和库活跃起来的原因。
因此,除非您想开始深入研究这些内容,否则您必须自己实现这种“活力”。一种(相当直接的)方法是修改您的类以存储对 DOM 对象的引用(而不仅仅是一个普通的字符串):
this.value = this.roll();
this.$div = $(`<div class="a-die d-flex align-items-center justify-content-center mx-4"><h1 class="die-text">${this.value}</h1></div>`);
$('#diceHolder').append(this.$div);
diceArray.push(this);
...然后使用强制 DOM 使用当前值更新的方法来丰富 Die 类:
refreshView = () => {
this.$div.find('h1').text(this.value);
}
之后,您可以直接在单击处理程序中更改值后调用此方法(注意它应该是一个正确的函数;现在您只调用一次 forEach):
$('#rollDice').on('click', () => diceArray.forEach(val => {
val.value = randomNumber(1, 7);
val.refreshView();
}));
这就是它的样子:
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
const diceArray = [];
const body = document.body;
class Die {
constructor() {
this.value = this.roll();
this.$div = $(`<div class="a-die d-flex align-items-center justify-content-center mx-4"><h1 class="die-text">${this.value}</h1></div>`);
diceArray.push(this);
$('#diceHolder').append(this.$div);
}
roll = () => {
return randomNumber(1, 7);
}
refreshView = () => {
this.$div.find('h1').text(this.value);
}
rollAll = () => {
this.value = randomNumber(1, 7);
this.refreshView();
}
}
$('#genDice').on('click', () => { let thisDie = new Die() });
$('#yahtzee').on('click', () => $('.die-text').text(randomNumber(1, 7)));
$('#rollDice').on('click', () => diceArray.forEach(val => {
// console.log(val);
val.value = randomNumber(1, 7);
val.refreshView();
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="diceHolder"></div>
<button id="genDice">Gen Dice</button>
<button id="rollDice">Reroll</button>
您可以获得更多创意,并组织您的代码,以便更新值始终触发 refreshView(例如,通过使用 Proxy)。或者您可以跟踪任何事件,并重新检查绑定到 DOM 的每个值,在发生更改时触发重新渲染。最后,您可以强制您的代码用户避免直接修改对象,而只调用特定函数来更新它(将“refreshView”连接到该函数)。
但是,在这种情况下,您实际上是开始自行重新实现这些框架。 ) 当你正在学习这些绳索时,这可能是一个好主意 - 但至少开始学习其中一个也可能是一个可靠的选择。