JavaScript 代码的奇怪问题
嗨,Web 开发大师, 首先我想说我不相信自己的眼睛 - 我有一段 javascript 在 IE7 中运行得很好,但在 Firefox 中却不行! :))))那是个小玩笑。 :) 所以我已经告诉过你这个问题了(这不是玩笑),现在我粘贴 javascript:
<script type="text/javascript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var ms;
ms = %%CONTENT_REFRESH%% - 5;
var stop;
stop = 0;
var myvalue;
function display() {
if (!stop) {
setTimeout("display();", 1000);
}
thetime.value = myvalue;
}
function recalc() {
var hours;
var minutes;
var seconds;
ms = ms - 1;
hours = Math.floor(ms / 3600);
minutes = Math.floor(ms / 60);
if (minutes < 10) {
minutes = "0"+minutes;
}
seconds = ms - (minutes*60) - (hours*3600);
if (seconds < 10) {
seconds = "0"+seconds;
}
myvalue = hours+":"+minutes+":"+seconds;
thetime.value = myvalue;
if (myvalue == "0:00:00") {
stop = 1;
}
if (!stop) {
setTimeout("recalc();", 1000);
}
}
// End -->
</SCRIPT>
这是我知道的非常旧的脚本。它需要我当前剩余的歌曲时间,来自我的 winamp 和网站倒计时。但正如我所说,它在 Firefox 中不起作用。
调用倒计时器的主体和代码如下所示:
<body class="playlist_body" onLoad="recalc();display();">
Time Left In Song: <INPUT align="center" TYPE="text" Name="thetime" size=5 />
</body>
//Edit: 我查看 FireBug,看到以下错误:
thetime is not defined
recalc()playlist.cgi (line 87)
function onload(event) { recalc(); display(); }(load )1 (line 2)
error source line: [Break on this error] thetime.value = myvalue;\n
Hi Masters Of Web Development,
first I want to say that I did not believe in my eyes - I've got a piece of javascript that works just fine in IE7, and don't in Firefox!!! :)))) That was little joke. :)
So I already told you the problem (it wasn't joke), now I'm pasting the javascript:
<script type="text/javascript">
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Begin
var ms;
ms = %%CONTENT_REFRESH%% - 5;
var stop;
stop = 0;
var myvalue;
function display() {
if (!stop) {
setTimeout("display();", 1000);
}
thetime.value = myvalue;
}
function recalc() {
var hours;
var minutes;
var seconds;
ms = ms - 1;
hours = Math.floor(ms / 3600);
minutes = Math.floor(ms / 60);
if (minutes < 10) {
minutes = "0"+minutes;
}
seconds = ms - (minutes*60) - (hours*3600);
if (seconds < 10) {
seconds = "0"+seconds;
}
myvalue = hours+":"+minutes+":"+seconds;
thetime.value = myvalue;
if (myvalue == "0:00:00") {
stop = 1;
}
if (!stop) {
setTimeout("recalc();", 1000);
}
}
// End -->
</SCRIPT>
This is very old script I know that. It takes my current remaining song time, from my winamp and countdowns in site. But as I said, it does not work in Firefox.
Body and code that calls countdown timer looks like this:
<body class="playlist_body" onLoad="recalc();display();">
Time Left In Song: <INPUT align="center" TYPE="text" Name="thetime" size=5 />
</body>
//Edit: I look at FireBug, and I saw the following error:
thetime is not defined
recalc()playlist.cgi (line 87)
function onload(event) { recalc(); display(); }(load )1 (line 2)
error source line: [Break on this error] thetime.value = myvalue;\n
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是它通过名称访问 DOM 元素。
将以下代码添加到顶部,为
thetime
元素声明一个变量,将id="thetime"
添加到INPUT
,并添加在body
元素的onload
中调用init();
。顺便说一句,您可以通过将
div
的 ID 设置为thetime
并替换来将文本框替换为常规
与DIV
元素。 thetime.valuethetime.innerHTML
。另外,最好使用函数而不是字符串来调用 setTimeout;您应该分别替换
"display();"
和"recalc();"
为display
和recalc
。The problem is that it's accessing DOM elements by name.
Add the following code to the top to declare a variable for the
thetime
element, addid="thetime"
to theINPUT
, and add a call toinit();
inonload
in thebody
element.By the way, you can replace the textbox with a regular
DIV
element by setting thediv
's ID tothetime
, and replacingthetime.value
withthetime.innerHTML
.Also, it's better to call setTimeout with a function instead of a string; you should replace
"display();"
and"recalc();"
withdisplay
andrecalc
respectively.IE 有一个“功能”,其中具有 name 属性的元素被放置在 window 对象中,例如。
会给你一个变量“foo”——这是非标准的,你应该这样做
来获取计时器输出元素。
IE has a "feature" where an element with a name attribute is placed in the window object, eg.
Will give you a variable "foo" -- this is non-standard, you should do
To get the timer output element.
并将 id="thetime" 添加到输入中,而不仅仅是 name="thetime"
and add id="thetime" instead of just name="thetime" to the input