本文介绍了将 Unicode 字符插入 JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 html 页面上插入一个 Omega (Ω).我使用它的 HTML 转义代码来做到这一点,所以我可以编写 Ω 并获得 Ω.当我把它放入一个 HTML 元素时,这一切都很好;但是,当我尝试将它放入我的 JS 中时,例如var Omega = Ω,它将代码解析为 JS 并且整个过程不起作用.有人知道怎么做吗?

I need to insert an Omega (Ω) onto my html page. I am using its HTML escaped code to do that, so I can write Ω and get Ω. That's all fine and well when I put it into a HTML element; however, when I try to put it into my JS, e.g. var Omega = Ω, it parses that code as JS and the whole thing doesn't work. Anyone know how to go about this?

推荐答案

我猜你真的希望 Omega 成为一个 包含 大写 omega 的字符串?在这种情况下,你可以写:

I'm guessing that you actually want Omega to be a string containing an uppercase omega? In that case, you can write:

var Omega = 'u03A9';

(因为 Ω 是代码点为 U+03A9 的 Unicode 字符;即即,03A9937,除了写成四个十六进制数字.)

(Because Ω is the Unicode character with codepoint U+03A9; that is, 03A9 is 937, except written as four hexadecimal digits.)

这篇关于将 Unicode 字符插入 JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 04:49