Javascript 对于带有属性的标记,document.write中使用的正确语法是什么?

Javascript 对于带有属性的标记,document.write中使用的正确语法是什么?,javascript,document.write,Javascript,Document.write,我在javascript的document.write方法中使用以下代码: <script type="text/javascript"> document.write("<font color="blue">["+time()+"]</font>"); </script> 文件。写入(“[”+时间()+“]”); 但这似乎不起作用。 请指导我。这个问题与HTML没有直接关系,只是与JavaScript(以及几乎所

我在javascript的document.write方法中使用以下代码:

    <script type="text/javascript">
      document.write("<font color="blue">["+time()+"]</font>");
    </script>

文件。写入(“[”+时间()+“]”);
但这似乎不起作用。
请指导我。

这个问题与HTML没有直接关系,只是与JavaScript(以及几乎所有其他编程语言)中字符串文字的工作方式有关。如果要在用
字符分隔的JavaScript字符串中使用
字符,则必须对其进行转义:
\“
。或者使用
来分隔字符串或属性值

document.write("<font color=\"blue\">["+time()+"]</font>");
document.write('<font color="blue">['+time()+"]</font>");
document.write("<font color='blue'>["+time()+"]</font>");
document.write(“[”+time()+“]);
document.write(“[”+time()+“]);
文件。写入(“[”+时间()+“]”);


也就是说,使用
document.write
生成HTML通常不是一个好主意(它只在加载时工作,通常最好用更可靠的服务器端代码或更可重用的DOM操作代替))
font
元素已被弃用。

这个问题与HTML没有直接关系,只是与JavaScript(以及几乎所有其他编程语言)中字符串文字的工作方式有关。如果您想在用
分隔的JavaScript字符串中使用
字符“
字符,则必须对其进行转义:
\”
。或者使用
来分隔字符串或属性值

document.write("<font color=\"blue\">["+time()+"]</font>");
document.write('<font color="blue">['+time()+"]</font>");
document.write("<font color='blue'>["+time()+"]</font>");
var color = 'blue';     
document.write("<font color="+color+">["+time()+"]</font>");
document.write(“[”+time()+“]);
document.write(“[”+time()+“]);
文件。写入(“[”+时间()+“]”);

这就是说,使用
document.write
生成HTML通常不是一个好主意(它只在加载时工作,通常最好用更可靠的服务器端代码或更可重用的DOM操作来代替),而且
font
元素已被弃用。

var color='blue';
var color = 'blue';     
document.write("<font color="+color+">["+time()+"]</font>");
文件。写入(“[”+时间()+“]”);

document.write(“[”+time()+“]);
var color='blue';
文件。写入(“[”+时间()+“]”);

document.write(“[”+time()+“]);

文件。写入(“[”+时间()+“]”);


document.write('['+time()+']');
请注意,
time
在JavaScript中不作为函数存在——我假设您在上面创建了一个未包含在代码片段中的函数。此外,您不应该真正使用
文档。除非您只是在进行实验,否则请编写任何内容的
。您可以使用Firefox的Firebug或Saf中的
控制台.log
来测试所有这些内容ari/Chrome Web Inspector的实验速度要比整版刷新快得多。我还注意到我是如何在
+
标志的两侧添加空格的:更容易阅读。


文件。写入(“[”+时间()+“]”);


document.write('['+time()+']');

请注意,
time
在JavaScript中不作为函数存在——我假设您在上面创建了一个未包含在代码片段中的函数。此外,您不应该真正使用
文档。除非您只是在进行实验,否则请编写任何内容的
。您可以使用Firefox的Firebug或Saf中的
控制台.log
来测试所有这些内容ari/Chrome Web Inspector的实验速度要比整版刷新快得多。我还注意到我是如何在
+
符号的两侧添加空格的:更易于阅读。

对于
document.write
来说,正确的语法是不使用它。对于
document.write
来说,正确的语法是不使用它。
<script type="text/javascript">
      document.write('<font color="blue">[' + time() + ']</font>');
</script>