admin管理员组文章数量:1277876
I am trying to list use the counter in a for loop as the number of a Unicode character.
To use a Unicode character in JavaScript one can either type it in as it is, or use an escape sequence like: \u8211
. My problem arises when I try to bine the number part with the escaped u. The error I get is something along the lines of "bad escape character", and it means that the number from the i
variable is not bined with the \u
as I'm hoping for.
for (var i=65; i< 90; i++ ) {
anchor = document.createElement('a'),
img = document.createElement('img'),
character = "\\u"+i;
img.setAttribute('alt', character);
img.setAttribute('src', '');
anchor.appendChild(document.createTextNode(i +": "));
anchor.appendChild(img);
anchor.setAttribute('title', character);
body.appendChild(anchor);
body.appendChild(document.createElement('br'));
}
What I have tried:
character = "\u{"+i+"}"
cha = ['\\u'];
cha.push(i);
cha.join('');
... and I've run out of ideas
An example:
body = document.getElementsByTagName('body')[0];
anchor = document.createElement('a');
img = document.createElement('img');
character = "\u8211";
img.setAttribute('alt', character);
img.setAttribute('src', '');
anchor.appendChild(document.createTextNode("x : "));
anchor.appendChild(img);
anchor.setAttribute('title', character);
body.appendChild(anchor);
body.appendChild(document.createElement('br'));
for (var i = 65; i < 87; i++) {
anchor = document.createElement('a');
img = document.createElement('img');
character = "\\u" + i;
//character = "\u" + i; << bad unicode escape
img.setAttribute('alt', character);
img.setAttribute('src', '');
console.log(img);
anchor.appendChild(document.createTextNode(i + ": "));
anchor.appendChild(img);
anchor.setAttribute('title', character);
body.appendChild(anchor);
body.appendChild(document.createElement('br'));
//console.log( tosay.join(''));
}
body {
font-size: 12px;
font-face: monotype;
}
<html>
<head>
<title>UTF chars</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
</html>
I am trying to list use the counter in a for loop as the number of a Unicode character.
To use a Unicode character in JavaScript one can either type it in as it is, or use an escape sequence like: \u8211
. My problem arises when I try to bine the number part with the escaped u. The error I get is something along the lines of "bad escape character", and it means that the number from the i
variable is not bined with the \u
as I'm hoping for.
for (var i=65; i< 90; i++ ) {
anchor = document.createElement('a'),
img = document.createElement('img'),
character = "\\u"+i;
img.setAttribute('alt', character);
img.setAttribute('src', '');
anchor.appendChild(document.createTextNode(i +": "));
anchor.appendChild(img);
anchor.setAttribute('title', character);
body.appendChild(anchor);
body.appendChild(document.createElement('br'));
}
What I have tried:
character = "\u{"+i+"}"
cha = ['\\u'];
cha.push(i);
cha.join('');
... and I've run out of ideas
An example:
body = document.getElementsByTagName('body')[0];
anchor = document.createElement('a');
img = document.createElement('img');
character = "\u8211";
img.setAttribute('alt', character);
img.setAttribute('src', '');
anchor.appendChild(document.createTextNode("x : "));
anchor.appendChild(img);
anchor.setAttribute('title', character);
body.appendChild(anchor);
body.appendChild(document.createElement('br'));
for (var i = 65; i < 87; i++) {
anchor = document.createElement('a');
img = document.createElement('img');
character = "\\u" + i;
//character = "\u" + i; << bad unicode escape
img.setAttribute('alt', character);
img.setAttribute('src', '');
console.log(img);
anchor.appendChild(document.createTextNode(i + ": "));
anchor.appendChild(img);
anchor.setAttribute('title', character);
body.appendChild(anchor);
body.appendChild(document.createElement('br'));
//console.log( tosay.join(''));
}
body {
font-size: 12px;
font-face: monotype;
}
<html>
<head>
<title>UTF chars</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
</body>
</html>
Share
Improve this question
edited Feb 6, 2023 at 4:06
Michael M.
11.1k11 gold badges21 silver badges43 bronze badges
asked Oct 5, 2012 at 12:28
Ярослав РахматуллинЯрослав Рахматуллин
7,9211 gold badge42 silver badges65 bronze badges
3
-
1
How about using
fromCharCode
instead of trying'\u'+i
. – Brad Christie Commented Oct 5, 2012 at 12:31 - I haven't really studied the javascript api. Thank you. – Ярослав Рахматуллин Commented Oct 5, 2012 at 12:40
- ☞ just feed this function two pairs of hex numbers: Yet Another JavaScript Unicode Encode/Decode have fun! ☕ – user257319 Commented Jan 3, 2016 at 3:48
4 Answers
Reset to default 8The biggest problem is that \uXXXX
is interpreted at the time that the code is parsed; just as you can't write '"' + '"'
to mean the same as ""
(because "
is an actual double-quote in the code, whereas '"'
is a string containing "
), you can't write '\\u' + 'XXXX'
to mean the same as '\uXXXX'
.
As Brad Christie says in a ment above, you should use the function String.fromCharCode
to convert from an integer to the character you need:
character = String.fromCharCode(i);
A second problem — this is academic, due to the above, but I think I should mention it — is that the \uXXXX
notation expects the character code to be given in hexadecimal notation, and zero-padded to exactly four hexadecimal digits, whereas you're giving it in decimal notation and without zero-padding. For example, you're writing \u65
, but the Unicode-escape syntax for A
is actually \u0041
.
Try this: String.fromCharCode(i);
.
character = String.fromCharCode(i); // works for me
Try something like this:
charCode = 2665;
uc = '"\\u' + charCode +'"';
document.write('<a title="' + charCode + '">' + eval(uc) + '</a>');
本文标签: javascriptGenerate a list of unicode characters in a for loopStack Overflow
版权声明:本文标题:javascript - Generate a list of unicode characters in a for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741294866a2370758.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论