这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Lab 5A : Fibbonacci numbers using Javascript</title>
</head>
<body>
<h1>Fibonacci Number Generation</h1>
<h2>Using Javascript</h2>
<hr />
<script type="text/javascript">
var n = prompt("Enter the number of terms : ", "") ;
var fib0 = 0, fib1 = 1;
var i;
var tmp ;
for ( i=2; i<=n; i++ )
{
var tmp = fib0;
fib0 = fib1;
fib1 = tmp + fib1;
}
document.write(n > 0 ? fib1 : 0);
</script>
</body>
</html>
我收到以下错误:
XML Parsing Error: not well-formed Location: file:///C:/Users/s13rw81/Desktop/Lab%20Programs/IWT%20Lab/Lab5A.xhtml
Line Number 17, Column 16:
//for ( i=2; i<=n; i++ )
---------------^
答案 0 :(得分:1)
我必须在这里同意epascarello,但是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<title>Lab 5A : Fibbonacci numbers using Javascript</title>
</head>
<body>
<h1>Fibonacci Number Generation</h1>
<h2>Using Javascript</h2>
<hr />
<script type="text/javascript">
//<![CDATA[
var n = prompt("Enter the number of terms : ", "") ;
n = parseInt(n);
// check input here not in the document.write
var fib0 = 0, fib1 = 1;
var i;
var tmp;
for (i=2; i<=n; i++ ) {
tmp = fib0;
fib0 = fib1;
fib1 = tmp + fib1;
}
alert(fib1);
//]]>
</script>
</body>
</html>
问题是Javascript中的低于字符的括号,但是HTML元素的开头字符。您可以将整个脚本放入CDATA
括号中,以避免因<
的每次出现而笨手笨脚。请关注Xufox的链接以了解更多信息。