将纯文本转换为HTML [英] Convert plain text in to HTML

查看:303
本文介绍了将纯文本转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,在某个时候我可以通过Ajax调用获取HTML(data).我必须将此HTML转换为纯文本,如下所示:

I have a script with which at a certain point I'm getting HTML(data) with Ajax call. I have to turn this HTML in to plain text like this:

$("#div").text(data);

我现在想解决这个问题,并再次使文本成为HTML.我有一个简单的Jquery方法吗?我试过了:

I now want to turn this around and make the text HTML again. I there an easy Jquery method of doing this? I tried:

$("#div").text(data).end().html();

没有运气.

推荐答案

您需要先保留原始响应才能执行此操作...

You would need to hold onto the original response to do this...

$("#hiddendiv").html(data);
$("#div").text(data);

然后您可以将其取回...

Then you could get it back out...

var html = $("#hiddendiv").html();

根据评论进行更新...

您可以在显示该元素之前将其删除...

You can remove that element before you display it...

var html = $(data);
$('#cookiediv', html).hide();
$('#div').html(html);

cookie消息中包含id="cookiediv"的位置-您可能需要调整选择器才能进入此div,但几乎可以肯定会抓住它.

Where the cookie message has id="cookiediv" - you may need to adjust the selector to get to this div, but it is almost certainly possible to grab it.

这篇关于将纯文本转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆