简体   繁体   English

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

[英]How to embed utf8 encoded html into element

I have a UTF-8 encoded string that contains html markup. 我有一个包含html标记的UTF-8编码的字符串。 I am not able to successfully embed that html into a div using jquery - it always ends up rendering as a string. 我无法使用jquery成功地将该html嵌入div中-它始终最终呈现为字符串。 I am restricted to v 1.7.2 so cant look at parseHTML function. 我仅限于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>

A fiddle is here - https://jsfiddle.net/carlv/ocmtaowz/2/ 小提琴在这里-https: //jsfiddle.net/carlv/ocmtaowz/2/

Am stumped - any suggestions most appreciated! 很困惑-任何建议,不胜感激!

Try making your function resultingCode use decodeURIComponent() 尝试使您的函数resultCode使用encodeURIComponent()

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");
}

Reference: decodeUriComponent 参考: decodeUriComponent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM