JavaScript 代码的奇怪问题

发布于 2024-08-08 20:50:05 字数 1646 浏览 4 评论 0原文

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

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

发布评论

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

评论(3

拿命拼未来 2024-08-15 20:50:05

问题是它通过名称访问 DOM 元素。

将以下代码添加到顶部,为 thetime 元素声明一个变量,将 id="thetime" 添加到 INPUT,并添加在 body 元素的 onload 中调用 init();

var thetime;

function init() {
    thetime = document.getElementById('thetime');
}

顺便说一句,您可以通过将 div 的 ID 设置为 thetime 并替换 来将文本框替换为常规 DIV 元素。 thetime.valuethetime.innerHTML

另外,最好使用函数而不是字符串来调用 setTimeout;您应该分别替换 "display();""recalc();"displayrecalc

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, add id="thetime" to the INPUT, and add a call to init(); in onload in the body element.

var thetime;

function init() {
    thetime = document.getElementById('thetime');
}

By the way, you can replace the textbox with a regular DIV element by setting the div's ID to thetime, and replacing thetime.value with thetime.innerHTML.

Also, it's better to call setTimeout with a function instead of a string; you should replace "display();" and "recalc();" with display and recalc respectively.

青萝楚歌 2024-08-15 20:50:05

IE 有一个“功能”,其中具有 name 属性的元素被放置在 window 对象中,例如。

<div name=foo></div>

会给你一个变量“foo”——这是非标准的,你应该这样做

document.getElementByName("foo") 

来获取计时器输出元素。

IE has a "feature" where an element with a name attribute is placed in the window object, eg.

<div name=foo></div>

Will give you a variable "foo" -- this is non-standard, you should do

document.getElementByName("foo") 

To get the timer output element.

娇纵 2024-08-15 20:50:05
var thetime = document.getElementById("thetime");

并将 id="thetime" 添加到输入中,而不仅仅是 name="thetime"

var thetime = document.getElementById("thetime");

and add id="thetime" instead of just name="thetime" to the input

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