Javascript解码特殊字符

时间:2015-06-25 08:34:06

标签: javascript character-encoding escaping decode

我从我的服务器收到public static LocalDateTime getLastFriday(LocalDateTime anchor) { LocalDateTime ldt = LocalDateTime.from(anchor); return ldt.with(DayOfWeek.FRIDAY).withHour(17).withMinute(42).withSecond(0).withNano(0); } public static LocalDateTime getNextSunday(LocalDateTime anchor) { LocalDateTime ldt = LocalDateTime.from(anchor); return ldt.with(DayOfWeek.SUNDAY).withHour(17).withMinute(42).withSecond(0).withNano(0); } 字符为"的字符串。 我想正确显示这个字符串,没有任何编码字符

所以我尝试使用decodeURI或unescape函数,如下所示:

"

购买仍然输出保持编码

decodeURI(""")
unescape(""")

有什么线索?

谢谢!

4 个答案:

答案 0 :(得分:0)

decodeURI()unescape()用于解码URI内容,而不是HTML。 我认为你正在混合你的技术。

如果jquery可用,那么这些可能对您有所帮助: Javascript decoding html entities How to decode HTML entities using jQuery?

答案 1 :(得分:0)

使用Jquery

$("myel").html(""");

将在屏幕上显示正确的字符串,而源为" \

答案 2 :(得分:0)

如果您希望仅对javascript版本进行解码,请使用以下命令:

function decodingFunctionVer(str) {
    var tmp = document.createElement("div");
    tmp.innerHTML = str;
    return tmp.textContent;
}

使用jsFiddle演示。

答案 3 :(得分:0)

解决了:

function decodeText(encodedText){
        var div = document.createElement('div');
        div.innerHTML = encodedText;
        var decoded = div.firstChild.nodeValue;
        return decoded;
    }

我不喜欢这个解决方案,因为它包括创建一个redundent元素,但它有效。

感谢您的帮助

相关问题