简体   繁体   English

javascript document.write 打印“<”

[英]javascript document.write printing “<"

我正在尝试在 javascript 中的 document.write 中打印此文本“<Place>” 。但我无法打印它。由于“<”符号,它没有打印。需要帮助。

document.write('&lt;Place>');

You have to represent the special character < , using the HTML entity &lt;您必须使用HTML 实体&lt;来表示特殊字符< &lt; . . Otherwise, your string will be interpreted as a HTML tag.否则,您的字符串将被解释为 HTML 标记。

I agree with @Paul Draper's answer as being a solution for this question.我同意@Paul Draper 的回答作为这个问题的解决方案。

However, you should be aware of characters that have special meaning in HTML and always convert special characters to character entities .但是,您应该了解 HTML 中具有特殊含义的字符,并始终将特殊字符转换为字符实体

If you are not careful and simply include the characters, than the browser will try to interpret them.如果您不小心并简单地包含字符,那么浏览器将尝试解释它们。 This could lead to potential issues and big headaches where unwanted HTML elements might get included down the line.这可能会导致潜在的问题和令人头疼的问题,其中不需要的 HTML 元素可能会被包含在内。

In this particular example it would be wise to encode both < and >在这个特定的例子中,同时对<>进行编码是明智的

document.write('&lt;Place&gt;');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM