如何定位具有相同功能的三个ID?(How can I target three IDs with the same function?)

编程入门 行业动态 更新时间:2024-10-24 20:13:29
如何定位具有相同功能的三个ID?(How can I target three IDs with the same function?)

我正在尝试将相同的功能应用于三个不同的ID。 我创建了一个小提琴来说明这一点: https : //jsfiddle.net/jnoweb/xrpsshcp/

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();

所以我试图以与红色数字相同的方式为黑色数字设置动画。 有任何想法吗?

谢谢!

I'm trying to apply the same function to three different IDs. I've created a fiddle to illustrate this: https://jsfiddle.net/jnoweb/xrpsshcp/

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();

So I'm trying to animate the black numbers in the same way as the red number. Any ideas?

Thanks!

最满意答案

第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/

Step 1: Make an Array of elements, store it in scoreDisplay like you did with the first element.

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

Step 2: perform your update function for each element, by using Array.prototype.forEach:

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

In your fiddle: https://jsfiddle.net/ku72qy7e/

更多推荐

本文发布于:2023-08-06 20:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1455682.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:功能   ID   target   IDs   function

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!