如何将utf8编码的html嵌入到元素中

时间:2015-07-24 02:27:49

标签: javascript jquery html utf-8

我有一个包含html标记的UTF-8编码字符串。我无法使用jquery成功地将该html嵌入到div中 - 它总是最终呈现为字符串。我被限制在v 1.7.2,因此无法查看parseHTML函数。

<div id="target"></div>
<script>
var tgt = $('#target');

tgt.html(resultingCode());


function resultingCode(){
    return '%3Ctable%20width%3D%22650%22%20class%3D%22deviceWidth%22%20border%3D%220%22%20cellpadding%3D%220%22%20cellspacing%3D%220%22%20align%3D%22center%22%20bgcolor%3D%22%23eeeeed%22%3E%20%3Ctr%3E%20%3Ctd%20valign%3D%22top%22%20style%3D%22padding%3A0%22%20bgcolor%3D%22%23ffffff%22%3E%20%3Cp%20style%3D%22mso-table-lspace%3A0%3Bmso-table-rspace%3A0%3B%20padding%3A0px%3B%20margin%3A0%3B%22%3E%20%3Ca%20href%3D%22%23%22%3E%3Cimg%20src%3D%22http%3A%2F%2Fplacehold.it%2F650x350%26text%3DFULL%22%20alt%3D%22%22%20border%3D%220%22%20style%3D%22display%3A%20block%3B%20width%3A100%25%3B%20height%3Aauto%3B%22%2F%3E%3C%2Fa%3E%20%3C%2Fp%3E%20%3C%2Ftd%3E%20%3C%2Ftr%3E%20%3C%2Ftable%3E';
}
</script>

这里有一个小提琴 - https://jsfiddle.net/carlv/ocmtaowz/2/

很难过 - 任何建议都非常感激!

1 个答案:

答案 0 :(得分:1)

尝试使用您的函数resultCode使用decodeURIComponent()

function resultingCode(){
    return decodeURIComponent("%3Ctable%20width%3D%22650%22%20class%3D%22deviceWidth%22%20border%3D%220%22%20cellpadding%3D%220%22%20cellspacing%3D%220%22%20align%3D%22center%22%20bgcolor%3D%22%23eeeeed%22%3E%20%3Ctr%3E%20%3Ctd%20valign%3D%22top%22%20style%3D%22padding%3A0%22%20bgcolor%3D%22%23ffffff%22%3E%20%3Cp%20style%3D%22mso-table-lspace%3A0%3Bmso-table-rspace%3A0%3B%20padding%3A0px%3B%20margin%3A0%3B%22%3E%20%3Ca%20href%3D%22%23%22%3E%3Cimg%20src%3D%22http%3A%2F%2Fplacehold.it%2F650x350%26text%3DFULL%22%20alt%3D%22%22%20border%3D%220%22%20style%3D%22display%3A%20block%3B%20width%3A100%25%3B%20height%3Aauto%3B%22%2F%3E%3C%2Fa%3E%20%3C%2Fp%3E%20%3C%2Ftd%3E%20%3C%2Ftr%3E%20%3C%2Ftable%3E");
}

参考:decodeUriComponent