尝试创建嵌套列表时可以使用document.writeln(使用Javascript)吗? [英] Is it possible to use document.writeln ( using Javascript) when trying to create a nested list?
问题描述
尝试创建嵌套列表时可以使用document.writeln吗? 我是学习javascript的非常基础的人,不知道我在做什么错.我需要使用document.writeln创建一个嵌套列表,这似乎可以正常工作.但是,当我尝试对其进行验证时,会出现很多错误,而且似乎无效.甚至有可能做我正在做的事情.例如.我的头(在脚本区域)有类似的东西:
Is it possible to use document.writeln when trying to create a nested list? I am at the very very basic of learning javascript and don't know what I am doing wrong. I need to create a nested list using document.writeln, which seems to work. But, when I try to get it verified, a get a bunch of errors and it doesn't seem to be valid. Is it even possible to do what I am doing. For example. I have in the head (in the script area) something like:
var number1 = {"Name": "Smith", "First Name": "John", "Number": "58"};
并且在我体内的脚本区域中:
and in the script area in the body I have:
document.writeln("<ul>");
document.writeln("<li>" + "Individual : ");
document.writeln("<ul>");
for (x in number 1) {
document.writeln("<li>" + number1[x] + "</li>");}
}
document.writeln("</li></ul></ul>");
推荐答案
好吧,这行是错误的:
document.writeln("</li></ul></ul>");
应该是:
document.writeln("</ul></li></ul>");
您还获得了一个额外的括号:
You've also got an extra closing brace:
for (x in number 1) {
document.writeln("<li>" + number1[x] + "</li>");} // Remove the extra } here
}
所以我正在使用的测试内容是:
So the test content I'm using is:
<!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 http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Test</title>
<meta name="description" content="asdf" />
<script type="text/javascript">
var number1 = {"Name": "Smith", "First Name": "John", "Number": "58"};
</script>
</head>
<body>
<h2>asdf</h2>
<script type="text/javascript">
//<![CDATA[
document.write("<ul>");
document.write("<li>" + "Individual : ");
document.write("<ul>");
for (x in number1) {
document.write("<li>" + number1[x] + "<\/li>");
}
document.write("<\/ul><\/li><\/ul>");
//]]>
</script>
</body>
</html>
示例现在可以验证并起作用.
Example now validates and works.
这篇关于尝试创建嵌套列表时可以使用document.writeln(使用Javascript)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!