如何正确地在HTML标题中使用JavaScript插入unicode? [英] How do I correctly insert unicode in an HTML title using JavaScript?

查看:107
本文介绍了如何正确地在HTML标题中使用JavaScript插入unicode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用JavaScript设置HTML页面的标题时,我看到了一些奇怪的行为。例如:如果我将html字符引用直接插入到标题中,则Unicode将正确呈现,例如:

 < title>&#21543 ;&安培;#20986;< /标题> 

但是,如果我尝试通过JavaScript使用HTML字符引用,某些东西似乎正在转换& to(& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; b

  function execTitleChange(){
document.title =&#21543;&#20986;;

$ / code>

(我应该注意这是一点点的猜测;当我反思在执行这个JavaScript函数后使用Firebug的DOM,这是我看到&而不是&的地方。)



如果在设置时使用\ u编码的Unicode字符然后再次正常工作:

pre $函数execTitleChange() \\\出;
}

事实上,\u编码的字符对我来说很有意义,我认为这就是JavaScript代表Unicode字符的方式,但我很难理解为什么在使用html字符引用时行为会有所不同。

解决方案

JavaScript字符串常量由JavaScript解析器解析。 HTML标签中的文本由HTML解析器解析。这两种语言(以及扩展它们的解析器)是不同的,特别是它们通过字符代码表示字符的方式不同。



因此,发现实际上的方式是:-)在JavaScript中使用 \u 转义符号,并使用HTML实体(& #nnnn; )。



edit —现在,当您谈论从JavaScript创建/插入HTML 时,情况会变得更加混乱。当您使用 .innerHTML 从JavaScript更新DOM时,您基本上将HTML源代码交给HTML解析器进行解释。出于这个原因,你可以使用JavaScript \u 转义或HTML实体,并且事情会起作用(除了痛苦的字符编码不匹配等问题)。



最后,请注意,JavaScript还提供 String.fromCharCode()函数来从数字字符代码构造字符串。 b $ b

I'm seeing some weird behavior when I'm setting the title of an HTML page using JavaScript. If I insert html character references directly into the title the Unicode renders correctly, for instance:

<title>&#21543;&#20986;</title>

But if I attempt to use html characters references via JavaScript, something seems to be converting the & to (& amp ;) (separating them so SO doesn't just turn it back into ampersand) and thus breaking the encoding, causing it to be rendered as the full coded string:

function execTitleChange() {
  document.title = "&#21543;&#20986;";
}

(I should note that this is a little bit of speculation; when I introspect the DOM using Firebug after executing this JavaScript function, that's where I see the & instead of &.)

If I use \u encoded Unicode characters when setting the value from JavaScript then everything works correctly again:

function execTitleChange() {
  document.title = "\u5427\u51fa";
}

The fact that \u encoded characters work kind of makes sense to me since I think that's how JavaScript represents Unicode characters but I'm stumped as to why the behavior would be different when using the html character references.

解决方案

JavaScript string constants are parsed by the JavaScript parser. Text inside HTML tags is parsed by the HTML parser. The two languages (and, by extension, their parsers) are different, and in particular they have different ways of representing characters by character code.

Thus, what you've discovered is the way reality actually is :-) Use the \u escape notation in JavaScript, and use HTML entities (&#nnnn;) in HTML/XML.

edit — now the situation can get even more confusing when you're talking about creating/inserting HTML from JavaScript. When you use .innerHTML to update the DOM from JavaScript, then you are basically handing over HTML source code to the HTML parser for interpretation. For that reason, you can use either JavaScript \u escapes or HTML entities, and things will work (excepting painful issues of character encoding mismatches etc).

Finally, note that JavaScript also provides the String.fromCharCode() function to construct strings from numeric character codes.

这篇关于如何正确地在HTML标题中使用JavaScript插入unicode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆