Javascript 处理 DOM 的挫败感,在 1 个地方有效,在其他地方似乎不起作用
问题
如果我取出评论,
//document.getElementById('formtimer').innerHTML = TimeStr; // Shows 00:15:00
效果会很好。所以,我把它注释掉,然后...
我想制作一个图形计数器,所以我进入 Photoshop 制作了 0-9 并尝试了这个...
var imgCounter = document.getElementById('formtimer');
imgCounter.src = "graphics/odometers/white and blue with black background/1.png";
当 1 从未被绘制时,我将其放入 HTML 中(以防万一我那里已经有一张图片了)...
<td class="tbl_col1"><div align="right" id="formtimer"><img src="graphics/odometers/white and blue with black background/0.png" /></div></td>
所以...它在屏幕上绘制了一个 0,并且 javascript 应该将其更改为 1。我认为路径不正确,所以我使用了它。我什至将下面的代码从另一个函数(有效)复制到这里 - 但它不起作用(“其他”工作代码的原始 ID 不同。当我将其复制到这个非工作代码中时,我将其更改为“formtimer” 错误
var img_sad = "graphics/signup/smiley-sad006.gif";
var imgUsername = document.getElementById('formtimer');
imgUsername.src = img_sad;
我很困惑为什么innerHTML 工作正常,而我的图形无法工作,我尝试在Firebug 中进行调试,但没有出现任何
。
PROBLEM
This works if I take out the comment
//document.getElementById('formtimer').innerHTML = TimeStr; // Shows 00:15:00
Works wonderfully. So, I comment that out and...
I wanted to make a graphical counter so I went into Photoshop and made 0-9 and tried this...
var imgCounter = document.getElementById('formtimer');
imgCounter.src = "graphics/odometers/white and blue with black background/1.png";
When the 1 was never drawn I put this in the HTML (just in case I had to have a picture already there)...
<td class="tbl_col1"><div align="right" id="formtimer"><img src="graphics/odometers/white and blue with black background/0.png" /></div></td>
SO... it draws a 0 on the screen, and the javascript should change it to a 1. I thought the path was incorrect so I played with that. I even copied the below code from another function (that works) to here - and it didnt work (the original ID for the "other" working code was different. I changed it to 'formtimer' when I copied it into this non-working script.
var img_sad = "graphics/signup/smiley-sad006.gif";
var imgUsername = document.getElementById('formtimer');
imgUsername.src = img_sad;
I'm confused why the innerHTML works fine, and my graphics don't work. I tried debugging in Firebug and no errors appear.
Please and thank you once again!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
document.getElementById('formtimer');
获取元素,
imgCounter.src = "..."
设置 src的属性,没有任何意义。
您应该改为获取
![]()
元素。您可以使用firstChild
属性或向![]()
元素添加 id 来执行此操作。document.getElementById('formtimer');
gets the<div>
element andimgCounter.src = "..."
sets the src attribute of the<div>
, which has no meaning.You should get the
<img>
element instead. You can do this using thefirstChild
property or adding an id to the<img>
element.