Javascript 无效的位置<;脚本>;HTML中的标记<;预处理>;标签

Javascript 无效的位置<;脚本>;HTML中的标记<;预处理>;标签,javascript,html,css,Javascript,Html,Css,我正在浏览完整参考第三版JavaScript中给出的示例 <body> <h1>Standard Whitespace Handling</h1> <script> // STRINGS AND (X)HTML document.write("Welcome to JavaScript strings.\n");

我正在浏览完整参考第三版JavaScript中给出的示例

            <body> 
            <h1>Standard Whitespace Handling</h1> 

            <script>
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   

            <h1>Preserved Whitespace</h1> 
            <pre>
            <script>           // in Eclipse IDE, at this line invalid location of tag(script)                                             
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   
            </pre>
            </body>
可以看到作者给出的O/p

            <body> 
            <h1>Standard Whitespace Handling</h1> 

            <script>
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   

            <h1>Preserved Whitespace</h1> 
            <pre>
            <script>           // in Eclipse IDE, at this line invalid location of tag(script)                                             
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   
            </pre>
            </body>

标准空白处理
//字符串和(X)HTML
write(“欢迎使用JavaScript字符串。\n”);
document.write(“此示例演示了嵌套引号,如下所示。”\n”);
文档。写下(“注意换行符(\\n’s)和“);
document.write(“使用转义序列。\n”);
document.write(“你可能想知道,\”这个嵌套引用行吗?\”);
document.write(“它将。\n”);
document.write(“以下是一些格式化数据的示例:\n\n”);
document.write(“\tCode\tValue\n”);
document.write(“\t\\n\t新行\n”);
document.write(“\t\tbackslash\n”);
document.write(“\t\\\”\t双引号\n\n”);
保留空格
//在Eclipse IDE中,标记(脚本)在此行的位置无效
//字符串和(X)HTML
write(“欢迎使用JavaScript字符串。\n”);
document.write(“此示例演示了嵌套引号,如下所示。”\n”);
文档。写下(“注意换行符(\\n’s)和“);
document.write(“使用转义序列。\n”);
document.write(“你可能想知道,\”这个嵌套引用行吗?\”);
document.write(“它将。\n”);
document.write(“以下是一些格式化数据的示例:\n\n”);
document.write(“\tCode\tValue\n”);
document.write(“\t\\n\t新行\n”);
document.write(“\t\tbackslash\n”);
document.write(“\t\\\”\t双引号\n\n”);
(十) HTML自动“折叠”多个空格字符一个空格。因此,例如,在HTML中包含多个连续的选项卡只显示为一个空格字符。在本例中,pre标记用于告诉浏览器 文本是预格式化的,不应折叠文本内部的空白。类似地,我们可以使用CSS空白属性修改标准的空白处理。使用pre可以在输出中正确显示示例中的选项卡

            <body> 
            <h1>Standard Whitespace Handling</h1> 

            <script>
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   

            <h1>Preserved Whitespace</h1> 
            <pre>
            <script>           // in Eclipse IDE, at this line invalid location of tag(script)                                             
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   
            </pre>
            </body>

那么,如何摆脱这个警告,我真的需要对此有所关注吗?我想我遗漏了一些东西,因为我有作者没有错的直觉?

pre
标记中加入
script
没有什么错。这只是EclipseIDE验证问题。如果在浏览器中使用此html,一切正常,不会显示任何警告

            <body> 
            <h1>Standard Whitespace Handling</h1> 

            <script>
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   

            <h1>Preserved Whitespace</h1> 
            <pre>
            <script>           // in Eclipse IDE, at this line invalid location of tag(script)                                             
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   
            </pre>
            </body>

另外,如果您想将脚本标记显示为pre标记内的“文本内容”,那么请查看以下问题:

pre
标记内使用
script
没有错。这只是EclipseIDE验证问题。如果在浏览器中使用此html,一切正常,不会显示任何警告

            <body> 
            <h1>Standard Whitespace Handling</h1> 

            <script>
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   

            <h1>Preserved Whitespace</h1> 
            <pre>
            <script>           // in Eclipse IDE, at this line invalid location of tag(script)                                             
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   
            </pre>
            </body>

另外,如果您想在pre-tag中将脚本标记显示为“文本内容”,那么可以看看这个问题:

这本书已经有11年历史了。我很惊讶它还没有变成化石。为自己找到一个不使用
document.write()
的新引用。现在没有一个明智的JS开发人员会编写这样的代码。这不是问题,他们会怎么写?我的意思是演示“hello world”示例。@jonasnas一种方法是在HTML中创建一个ID为的
div
,并向
pre
元素添加ID,使用脚本使用变量和字符串连接构建字符串,然后使用
getElementById()将字符串分配给页面元素
.innerHTML
。我认为你不会找到一本在过去5年中编写的令人尊敬的JS简介书,它使用
document.write()
来创建页面内容(向子窗口写入内容除外)@JLRishe:我已经更新了我拥有的电子书信息。@JLRishe个人认为我不介意使用document.write'hello world'示例。就像本例一样,本书的主题是关于预标记,而不是js的最佳实践。另外,即使在《权威指南》一书中,它也提到使用dom函数是更好的方法(如您所建议的),但在同一本书中,作者使用document.write(“某些标记”)只是为了演示书中谈到的其他功能的一些行为,这本书已经有11年的历史了。我很惊讶它还没有变成化石。为自己找到一个不使用
document.write()
的新引用。现在没有一个明智的JS开发人员会编写这样的代码。这不是问题,他们会怎么写?我的意思是演示“hello world”示例。@jonasnas一种方法是在HTML中创建一个ID为的
div
,并向
pre
元素添加ID,使用脚本使用变量和字符串连接构建字符串,然后使用
getElementById()将字符串分配给页面元素
.innerHTML
。我认为你不会找到一本在过去5年中编写的令人尊敬的JS简介书,它使用
document.write()
来创建页面内容(向子窗口写入内容除外)@JLRishe:我已经更新了我拥有的电子书信息。@JLRishe个人认为我不介意使用document.write'hello world'示例。就像本例一样,本书的主题是关于预标记,而不是js的最佳实践。另外,即使在《权威指南》一书中,它也提到使用dom函数是b
            <body> 
            <h1>Standard Whitespace Handling</h1> 

            <script>
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   

            <h1>Preserved Whitespace</h1> 
            <pre>
            <script>           // in Eclipse IDE, at this line invalid location of tag(script)                                             
            // STRINGS AND (X)HTML
            document.write("Welcome to JavaScript strings.\n");
            document.write("This example illustrates nested quotes 'like this.'\n");        
            document.write("Note how newlines (\\n's) and ");
            document.write("escape sequences are used.\n");
            document.write("You might wonder, \"Will this nested quoting work?\"");
            document.write(" It will.\n");
            document.write("Here's an example of some formatted data:\n\n");
            document.write("\tCode\tValue\n");
            document.write("\t\\n\tnewline\n");
            document.write("\t\\\\\tbackslash\n");
            document.write("\t\\\"\tdouble quote\n\n");
            </script>   
            </pre>
            </body>