Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试创建嵌套列表时是否可以使用document.writeln(使用Javascript)?_Javascript_List_Nested - Fatal编程技术网

尝试创建嵌套列表时是否可以使用document.writeln(使用Javascript)?

尝试创建嵌套列表时是否可以使用document.writeln(使用Javascript)?,javascript,list,nested,Javascript,List,Nested,尝试创建嵌套列表时是否可以使用document.writeln? 我在学习javascript的基础上,不知道自己做错了什么。我需要使用document.writeln创建一个嵌套列表,这似乎很有效。但是,当我试图验证它时,会发现一堆错误,而它似乎是无效的。甚至有可能做我正在做的事情。例如我的头部(脚本区域)有如下内容: var number1 = {"Name": "Smith", "First Name": "John", "Number": "58"}; 在正文中的脚本区域,我有: do

尝试创建嵌套列表时是否可以使用document.writeln? 我在学习javascript的基础上,不知道自己做错了什么。我需要使用document.writeln创建一个嵌套列表,这似乎很有效。但是,当我试图验证它时,会发现一堆错误,而它似乎是无效的。甚至有可能做我正在做的事情。例如我的头部(脚本区域)有如下内容:

var number1 = {"Name": "Smith", "First Name": "John", "Number": "58"};
在正文中的脚本区域,我有:

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(
    ); 书面文件(“
  • ”+“个人:”); 书面文件(“
      ”); 用于(数字1中的x){ document.writeln(“
    • ”+number1[x]+“
    • ”;) } 书面文件(“
”);
for(1号中的x)的
中的数字和1之间的空格使得此JavaScript无效。此外,关闭元素的顺序错误

document.writeln("</li></ul></ul>");
此输入将导致站点布局被破坏。通常,使用
document.writeln
直接编写用户输入允许您网站的用户通过利用


不要使用
document.write
,而是使用and/or.

好吧,这行是错误的:

document.writeln("</li></ul></ul>");
document.writeln(“”);
这应该是:

document.writeln("</ul></li></ul>");
document.writeln(“”);
编辑:您还有一个额外的右括号:

for (x in number 1) {
   document.writeln("<li>" + number1[x] + "</li>");} // Remove the extra } here
}
for(数字1中的x){
document.writeln(“
  • ”+number1[x]+“
  • ”;}//在此处删除多余的} }
    所以我使用的测试内容是:

    <!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>
    
    
    试验
    变量number1={“姓名”:“史密斯”,“名字”:“约翰”,“号码”:“58”};
    asdf
    //
    

    示例现在可以验证并起作用。

    当您尝试验证时,会出现什么错误?我很想标记为重复,因为有另一个,但我不知道这是否真的是重复的,因为重新发布-并不是说需要document.write,所以有人可以用“正确”的方式来回答(w/o document.write)并接受了,但不是这个@金姆,为了简单起见,你可能只想结束这个问题,因为你的另一个问题更完整。哦,这是我编的。我实际上使用了不同的变量,但我知道你的意思。把它想象成数字1。我添加了一个很长的更新,解释了为什么使用
    document.writeln
    通常不是一个好主意(还有一个关于结束标记的错误顺序的注释)。这很有帮助,我现在有4个错误。但仍在努力解决其余问题。伙计,我觉得就像jforberg在评论中建议的那样,发布你收到的错误将有助于解决其他错误。第58行第28列:文档类型不允许元素“ul”出现在文档中。write(
      );第58行第29列:文档中不允许使用字符数据;第60行第29列:文档中不允许使用字符数据;第62行第53列:这里不允许字符数据{document.write(“
    • ”+number1[x]+“
    • ”);基于此,我认为我们需要进一步的上下文。上面是什么内容/html?
      for (x in number 1) {
         document.writeln("<li>" + number1[x] + "</li>");} // Remove the extra } here
      }
      
      <!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>