Javascript 显示滚动消息-不';行不通

Javascript 显示滚动消息-不';行不通,javascript,Javascript,我是JavaScript的初学者,并尝试根据这一点编写简单的核心示例 但是这个变体滚动消息不起作用。我不明白为什么会这样。 我的网络浏览器googlechrome,编码ANSI 代码: <html> <head><title>Scrolling Message Example</title> <script> msg = "This is an example of a scrolling

我是JavaScript的初学者,并尝试根据这一点编写简单的核心示例

但是这个变体滚动消息不起作用。我不明白为什么会这样。 我的网络浏览器
googlechrome
,编码
ANSI

代码:

<html>
    <head><title>Scrolling Message Example</title>
        <script>
            msg = "This is an example of a scrolling message. Isn't it exciting?";
            msg = "...          ..." + msg;
            pos = 0;

            function ScrollMessage() {
               window.status = msg.substring(pos, msg.length) + msg.substring(0, pos);
               pos++;
               if (pos > msg.length) pos = 0;
               window.setTimeout("ScrollMessage()", 200);
            }

            ScrollMessage();
    </script>
        </head>
        <body>
            <h1>Scrolling Message Example</h1>
            Look at the status line at the bottom of this page. (Don't
            watch it too long, as it may be hypnotic.)
        </body>
</html>

滚动消息示例
msg=“这是滚动消息的一个示例。这不是很令人兴奋吗?”;
msg=“……”+msg;
pos=0;
函数ScrollMessage(){
window.status=msg.substring(pos,msg.length)+msg.substring(0,pos);
pos++;
如果(位置>消息长度)位置=0;
setTimeout(“ScrollMessage()”,200);
}
滚动消息();
滚动消息示例
查看本页底部的状态行。(不要
看得太久,因为它可能会催眠。)
问题:

  • 你如何解决这个问题
  • 为实现这一目标,哪些其他变体更适合使用
请参阅这篇关于

出于安全原因,默认情况下,大多数浏览器都禁用了Aparently window.status

我假设这个练习是关于滚动效果的,而不是关于状态栏

如果你换这个

window.status = msg.substring(pos, msg.length) + msg.substring(0, pos);

您将在控制台中看到,创建滚动效果的功能本身工作正常

如果要显示滚动消息,只需在页面中创建一个
,以便在每次调用滚动函数时显示消息并更新div的内容

我修改了你的例子:

<html>
    <head><title>Scrolling Message Example</title>
        <script>
            var msg = "This is an example of a scrolling message. Isn't it exciting?";
            msg = "...          ..." + msg;
            var pos = 0;

            function ScrollMessage() {
                document.getElementById("scrollMsgContainer").innerHTML = msg.substring(pos, msg.length) + msg.substring(0, pos);
                pos++;
                if (pos > msg.length) pos = 0;
                window.setTimeout(ScrollMessage, 200);
            }
        </script>
    </head>
    <body>
        <h1>Scrolling Message Example</h1>
        Look at the status line at the bottom of this page. (Don't
        watch it too long, as it may be hypnotic.)

        <div id="scrollMsgContainer" style="position: fixed; bottom: 0; width: 100%; background-color: #DDDDDD">&nbsp;</div>
    </body>
    <script>
        ScrollMessage();
    </script>
</html>

滚动消息示例
var msg=“这是一个滚动消息的示例。这不是很令人兴奋吗?”;
msg=“……”+msg;
var-pos=0;
函数ScrollMessage(){
document.getElementById(“scrollMsgContainer”).innerHTML=msg.substring(pos,msg.length)+msg.substring(0,pos);
pos++;
如果(位置>消息长度)位置=0;
设置超时(滚动消息,200);
}
滚动消息示例
查看本页底部的状态行。(不要
看得太久,因为它可能会催眠。)
滚动消息();

为什么不使用
文档。标题

function ScrollMessage() {
               document.title = msg.substring(pos, msg.length) + msg.substring(0, pos);
               pos++;
               if (pos > msg.length) pos = 0;
               window.setTimeout("ScrollMessage()", 200);
            }
除了IECheck reference之外,所有浏览器都支持它。 Google Chrome没有状态栏,而Mozilla Firefox默认阻止状态栏的更改

您可以在HTML中尝试一个,并设置它的内容


希望对您有所帮助。

您是否尝试过:text哦,请不要使用
字幕
@Lee Taloy为什么不使用marquee?那个教程已经过时了<代码>窗口。任何浏览器都不再支持状态。另外,将字符串传递给
setTimeout
是一种不好的做法(它使用
eval
);传递函数:
window.setTimeout(ScrollMessage,200)选框显然已被弃用。所以您可以使用@Rocket Hazmat I did this替换
console.log(msg.substring(pos,msg.length)+msg.substring(0,pos))-消息显示在控制台中。但我不明白关于
的最后一个答案。你能展示规避这个问题的代码吗?你真的应该在
msg
pos
之前添加
var
(哇,那个教程真的需要更新)。@Nazar,我相信(尽管过时)教程的重要部分不是关于状态栏,而是关于锻炼。
div
确实是页面中的一个区域。它仅用于显示滚动文本。我用它来替换状态栏。@Iljaas如何将window.status以正确的方式更改为Google Grome?如何将其更改为chrom?哪些要点需要改变?
function ScrollMessage() {
               document.title = msg.substring(pos, msg.length) + msg.substring(0, pos);
               pos++;
               if (pos > msg.length) pos = 0;
               window.setTimeout("ScrollMessage()", 200);
            }