Javascript 处理 DOM 的挫败感,在 1 个地方有效,在其他地方似乎不起作用

发布于 2024-12-19 03:10:17 字数 1041 浏览 3 评论 0原文

问题

如果我取出评论,

    //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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无尽的现实 2024-12-26 03:10:17

document.getElementById('formtimer'); 获取

元素,imgCounter.src = "..." 设置 src

的属性,没有任何意义。

您应该改为获取 元素。您可以使用 firstChild 属性或向 元素添加 id 来执行此操作。

document.getElementById('formtimer'); gets the <div> element and imgCounter.src = "..." sets the src attribute of the <div>, which has no meaning.

You should get the <img> element instead. You can do this using the firstChild property or adding an id to the <img> element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文