Different processing of Unicode characters by HTML, CSS and JS

Source: Internet
Author: User

Different representations of Unicode characters

Unicode characters are represented in HTML, CSS, and JS in different ways, as described below.

The original is published here

CSS notation

First, a very common font icon code for bootstrap:

.glyphicon-home:before { content: "\e021";}

The code above is the e021 Unicode code for this character, which is 16 binary.

Grammar:

‘\ + 16进制的unicode编码‘

such as: ' \5b89 ' denotes the Chinese character "Ann":

<div class=  "test"  ></div><< Span class= "Hljs-name" >style> .test:  Before {content:   "\e021" </STYLE>    
JS Representation method

Grammar:

‘\u + 16进制的unicode编码‘

Example:

// 如:‘\u5b89‘表示汉字“安”console.log(‘\u5b89‘); // 输出“安”
HTML representation method

HTML special Point, the use of 10 in the system , at first did not pay attention to this to make a day out not to be depressed for a long time.

Grammar:

‘&# + 10进制的unicode编码 + 英文分号;‘

such as & #23433; (note, because the normal writing will be converted to Chinese characters, so I deliberately left a space in the middle to prevent conversion) , the end of the semicolon after the test does not add, but it is better to add a bit.

<div>    这是unicode表示的字符“安”:& #23433;(这里故意留一空格防止转换)</div>

In addition, some special characters have other representations, which is often said html转义字符 , such as (because of the automatic conversion problem, here is represented by the picture):

The complete HTML escape character table can be found here:

http://tool.oschina.net/commons?type=2

Get Unicode encoding of Chinese characters

So, how do you know the Unicode encoding of a Chinese character? Very simple:

‘安‘.charCodeAt(); // 输出的 23433 就是汉字 安 的unicode编码,不过注意是10进制的String.fromCharCode(23433); // 输出 ‘安‘

Obtained 10 Unicode encoding, and then if you want to use JS and CSS inside, you need to use the toString(16) turn 16 to do further processing.

var Unicode=‘\\U+charcodeat (). tostring (16) //output string: "\u8317" JSON. parse ( ' "' +unicode +//output Kanji: "Tea" eval ( "' +unicode+//or using eval parsing can also          

HTML, CSS, JS different processing of Unicode characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.