如何使用相同功能定位三个ID?

问题描述:

我试图将相同的功能应用于三个不同的ID。我创建了一个小提琴来说明这个:https://jsfiddle.net/jnoweb/xrpsshcp/如何使用相同功能定位三个ID?

var game = {score:0}, 
scoreDisplay = document.getElementById("score1"); 

function add20() { 
    TweenLite.to(game, 1, { 
     score:"+=20", 
     roundProps:"score", 
     onUpdate:updateHandler, 
     ease:Linear.easeNone 
    }); 
} 

function updateHandler() { 
    scoreDisplay.innerHTML = game.score; 
} 

add20(); 

所以我想以同样的方式作为红色数字动画的黑色数字。有任何想法吗?

谢谢!

步骤1:请元素的Array,将其存储在scoreDisplay喜欢你的第一个元素一样。

scoreDisplay = [ 
    document.getElementById("score1"), 
    document.getElementById("score2"), 
    document.getElementById("score3") 
]; 

步骤2:执行您的更新函数的每个元素,通过使用Array.prototype.forEach

function updateHandler() { 
    scoreDisplay.forEach(function(display) { 
    display.innerHTML = game.score; 
    }); 
} 

在您的提琴:https://jsfiddle.net/ku72qy7e/