我在 js cookie 中有一个 html 文本。当我尝试获取 cookie 中的文本并将该文本设置在 html div 中时,它会被编码。我尝试解码 html 但没有用。
这是我的代码:
function addToCoupon(team1,team2,id,bet,rate){
var betTableContent=$('#bet_table').html();
var row="<tr><td>"+team1+"-"+team2+"</td><td>"+bet+"</td><td>"+rate+"</td></tr>";
betTableContent+=row;
$.cookie("coupon",betTableContent);
$('#bet_table').html(unescape($.cookie("coupon")));
}
如果我使用 unescape() 函数,#bet_table 中的文本看起来并不重要:
%3Ctr%3E%3Ctd%3EHapoel%20Nazareth%20Elite-Maccabi%20Herzliya%3C%2Ftd%3E%3Ctd%3E1X2
那么我怎样才能将这个html文本显示为普通的html呢?
最佳答案
在创建它之前对其进行转义,然后像这样取消转义
jQuery.cookie('coupon', escape('<html></html>'));
console.log(unescape(jQuery.cookie('coupon')));
关于javascript - 在Js中cookie html标签消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744285/