uoryon
V2EX  ›  问与答

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

  •  
  •   uoryon · May 9, 2012 · 3535 views
    This topic created in 5131 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
        1
    jiyinyiyong  
       May 9, 2012
    转码.. "string".chatCodeAt() 吗?
    clowwindy
        2
    clowwindy  
       May 9, 2012
    请把所谓的“unicode代码”贴出来看看是什么。
    uoryon
        3
    uoryon  
    OP
       May 9, 2012
    @jiyinyiyong 用不鸟。
    uoryon
        4
    uoryon  
    OP
       May 9, 2012
    @clowwindy \u4f60\u8bd5\u8bd5
    uoryon
        5
    uoryon  
    OP
       May 9, 2012
    @clowwindy 这个填到那个里面,仍然弹不出转码的汉字。。
    clowwindy
        6
    clowwindy  
       May 9, 2012   ❤️ 1
    https://gist.github.com/2643869

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