有没有一种简单的方法可以通过“imgCounter.src =”将多个javascript图像添加在一起
在 Photoshop 中,我创建了 0.png 到 9.png 的图像,并且知道我可以通过在倒计时时更改每个数字的每张图片来更改每个数字来模拟计数器。
IDEA 1:
我认为的一种方法是使用多行
HTML:
<div align="right"><img src="graphics/odometers/1.png" /id="digit1">
<img src="graphics/odometers/2.png" /id="digit2">
<img src="graphics/odometers/3.png" /id="digit3"></div>
JAVASCRIPT:
var imgCounter = document.getElementById('digit1');
imgCounter.src = "graphics/odometers/white and blue with black background/1.png"; // shows the digit 1
var imgCounter = document.getElementById('digit2');
imgCounter.src = "graphics/odometers/white and blue with black background/2.png"; // shows the digit 2
// etc for as many digits as I want to show
并按每个ID更改javascript中的数字。这并不难,但是为了灵活性,我可以在 javascript 中将 1 个图像 ID 和字符串 3 .png 放在一起吗?像这样...
IDEA 2:不确定这是否可能。
HTML:
<div align="right"><img src="" /id="formtimer"></div>
JAVASCRIPT:
// somehow show 3 graphics in a row 123 without having 3 ID tags, only 1 ID tag
var imgCounter = document.getElementById('formtimer');
imgCounter.src = "graphics/odometers/white and blue with black background/1.png";
我知道上面的例子只显示了数字1,并没有显示所有3个数字......(因为我不知道这是否可能)。
有没有办法同时显示3张图片?哈哈。容易地?我知道任何事情在技术上都可以通过困难的方式完成...
如果这是一个很大的痛苦,我可以接受 IDEA 1,但它不太“自由”,因为我必须使用多个 ID 标签对 HTML 进行编码,这很乏味。 只是好奇大家
的想法以及是否有解决方案。 :) 非常感谢。
ALSO:
如果我稍后要通过 JavaScript 粘贴图像,可以将 img src=""
保留为 NULL 或空字符串吗?
In photoshop I created images 0.png thru 9.png and know I can change each digit to simulate a counter by changing each picture of each digit as it counts down.
IDEA 1:
One way I thought would be to have multiple lines of
HTML:
<div align="right"><img src="graphics/odometers/1.png" /id="digit1">
<img src="graphics/odometers/2.png" /id="digit2">
<img src="graphics/odometers/3.png" /id="digit3"></div>
JAVASCRIPT:
var imgCounter = document.getElementById('digit1');
imgCounter.src = "graphics/odometers/white and blue with black background/1.png"; // shows the digit 1
var imgCounter = document.getElementById('digit2');
imgCounter.src = "graphics/odometers/white and blue with black background/2.png"; // shows the digit 2
// etc for as many digits as I want to show
and change the digits in javascript by each ID. It's not hard but for flexibility can I have 1 image ID and string 3 .png's together in javascript? Like this...
IDEA 2: Not sure if this is possible.
HTML:
<div align="right"><img src="" /id="formtimer"></div>
JAVASCRIPT:
// somehow show 3 graphics in a row 123 without having 3 ID tags, only 1 ID tag
var imgCounter = document.getElementById('formtimer');
imgCounter.src = "graphics/odometers/white and blue with black background/1.png";
I know the example above only shows the digit 1 and doesn't do all 3 digits... (because I don't know if it's possible).
Is there a way to display 3 pictures together? lol. Easily? I know anything can technically be done the hard way...
If it's a big pain in the butt I'm ok with IDEA 1 but it's less "freedom" because I have to encode the HTML with multiple ID tags which is tedius...
Just curious what you all think and if you have a solution. :) Much appreciated.
ALSO:
Is it ok to leave the img src=""
as a NULL or empty string if I'll later be sticking an image in via javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您根本不需要在源 html 中包含
![]()
元素,您可以使用 JavaScript 添加它们,甚至不需要给它们 id。在源代码中您想要计数器的位置,只需放置一个具有适当 id 的空 div 即可。
当使用容器 div 的 id 和值调用下面的
counterSet()
函数时,它会在 div 中创建新的子![]()
元素,并适当设置 src,但会重用之前设置的任何值中存在的任何现有子![]()
元素。如果新值的位数少于最后一个值,则该函数将删除剩余的![]()
元素:You don't need to include the
<img>
elements in the source html at all, you can add them with JavaScript and not even give them ids.At the point in your source where you want the counter just put an empty div with an appropriate id.
When the
counterSet()
function below is called with the id of the container div and a value it creates new child<img>
elements in the div with src set appropriately, but reuses any existing child<img>
elements that are there from any previous value that was set. If the new value has fewer digits than the last than the function removes the leftover<img>
elements:我只会使用三个单独的 id。
您可以使用类似的方法来设置 src 属性:
I would just use three separate ids.
You can use something like this to set the src attributes: