gpt4 book ai didi

javascript - 显示滚动消息 - 不起作用

转载 作者:行者123 更新时间:2023-11-30 05:49:28 26 4
gpt4 key购买 nike

我是 JavaScript 的初学者,并尝试根据这个做简单的核心示例 Workshop: Displaying a Scrolling Message .

但是这个变体滚动消息不起作用。我不明白为什么会这样。我的网络浏览器 Google Chrome,编码 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>

问题:

  • 如何解决这个麻烦?
  • 哪些其他变体更适合用于此目的?

最佳答案

查看关于 JavaScript window.status 的帖子

显然,出于安全原因,大多数浏览器默认禁用 window.status。

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

如果你替换这个

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

console.log(msg.substring(pos, msg.length) + msg.substring(0, pos));

您将在控制台中看到创建滚动效果的函数本身运行良好。

如果要显示滚动信息,只需要创建一个<div>在您要显示消息的页面中,并在每次调用滚动功能时更新 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>

关于javascript - 显示滚动消息 - 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15835720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com