gpt4 book ai didi

JavaScript 无法在下面的 html 代码中被读取

转载 作者:行者123 更新时间:2023-12-01 01:46:31 26 4
gpt4 key购买 nike

我是 javascript 新手,正在从书本中学习它。我尝试使用以下 HTML 和 js 代码打开网页。但 javascript 部分似乎不起作用。
有人可以告诉我缺少什么吗?

<head>
<title>Dynamic Page</title>
<script type="text/javascript">
var message = "Learning JavaScript will give your Web
page life!";
message += " Are you ready to learn? ";
var space = "...";
var position = 0;

function scroller() {
var newtext = message.substring(position, message.length) +
space + message.substring(0, position);
var td = document.getElementById("tabledata");
td.firstChild.nodeValue = newtext;
position++;
if (position > message.length) {
position = 0;
}
window.setTimeout(scroller, 200);
}
</script>
</head>

<body bgColor="darkgreen" onload="scroller();">
<table border="1">
<tr>
<td id="tabledata" bgcolor="white">message goes here</td>
</tr>
</table>
</body>

最佳答案

您的代码很好并且可以工作,只是有一些小格式问题,您不能在字符串文字中放置换行符。修正示例如下:

var message="Learning JavaScript will give your Web page life!";
message += " Are you ready to learn? ";
var space="...";
var position=0;

function scroller(){
var newtext = message.substring(position,message.length)+ space + message.substring(0,position);

var td = document.getElementById("tabledata");
td.firstChild.nodeValue = newtext;
position++;
if (position > message.length){position=0;}
window.setTimeout(scroller,200);
}

scroller();
<table border="1">
<tr>
<td id="tabledata" bgcolor="white">message goes here</td>
</tr>
</table>

关于JavaScript 无法在下面的 html 代码中被读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910711/

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