uoryon
V2EX  ›  问与答

求教高手,一个JS问题,如何转unicode码

  •  
  •   uoryon · May 9, 2012 · 3815 views
    This topic created in 5303 days ago, the information mentioned may be changed or developed.
    <input type="text" id="content">
    <input type="button" onclick="showMyheart()"/>

    <script>
    function showMyheart(){
    var text = document.getElementById('content');
    alert(text.value);
    }

    上面这段代码,如果填入unicode代码的话,还是alert unicode代码。。。求帮助,怎么改能转码
    12 replies  •  1970-01-01 08:00:00 +08:00
    jiyinyiyong
    jiyinyiyong
    May 9, 2012
    1
    转码.. "string".chatCodeAt() 吗?
    clowwindy
    clowwindy
    May 9, 2012
    2
    请把所谓的“unicode代码”贴出来看看是什么。
    uoryon
    uoryon
    OP
    May 9, 2012
    3
    @jiyinyiyong 用不鸟。
    uoryon
    uoryon
    OP
    May 9, 2012
    4
    @clowwindy \u4f60\u8bd5\u8bd5
    uoryon
    uoryon
    OP
    May 9, 2012
    5
    @clowwindy 这个填到那个里面,仍然弹不出转码的汉字。。
    clowwindy
    clowwindy
    May 9, 2012❤️1
    6
    https://gist.github.com/2643869

    不过我更想知道你为什么问这个问题,你肯定是别的地方弄错了什么,才有这么奇怪的需求。
    Paranoid
    Paranoid
    May 9, 2012
    7
    扔到浏览器地址栏中: javascript:alert("\u4f60\u8bd5\u8bd5"); ie6, chrome 高版本,firefox高版本 均无问题
    zern
    zern
    May 9, 2012
    8
    unescape(string)
    zern
    zern
    May 9, 2012
    9
    不过先要转成%u4f60这样的
    uoryon
    uoryon
    OP
    May 9, 2012
    10
    @clowwindy 。。。。不好意思说,涉及到柔弱内向的男生的心。。。。
    jiyinyiyong
    jiyinyiyong
    May 9, 2012
    11
    @uoryon 我打错字... 不大熟, 看下这个思路行不:
    var a = "你".charCodeAt(); // 取对应的码
    var b = a.toString(16); // 转 16 进制字符串
    var c = "\\u" + b; // 查了下是没错.. 不确定
    jiyinyiyong
    jiyinyiyong
    May 9, 2012
    12
    晕.. 反了, 从 unescape.. 无视我
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5071 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 03:01 · PVG 11:01 · LAX 19:01 · JFK 22:01
    ♥ Do have faith in what you're doing.