最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Raw html from string in jquery? - Stack Overflow

programmeradmin2浏览0评论

I have <label class='ash'>Comment Removed</label> in the database.

When I show this on the grid. I get this on the page:

<label class='ash'>Removed</label>

Actually I should just get Removed in Gray color

How can I convert this to Html like I do in MVC 3 Razor view?

@Html.Raw(HttpUtility.HtmlDecode(comment.txt)) works fine

I am using jquery 1.6 on MVC 3

I tried:

$("<label class='ash'>Comment Removed</label>").html()
   unescape($(txt)).html()

May be it is simple, but can't figure it out

I have &lt;label class='ash'&gt;Comment Removed&lt;/label&gt; in the database.

When I show this on the grid. I get this on the page:

<label class='ash'>Removed</label>

Actually I should just get Removed in Gray color

How can I convert this to Html like I do in MVC 3 Razor view?

@Html.Raw(HttpUtility.HtmlDecode(comment.txt)) works fine

I am using jquery 1.6 on MVC 3

I tried:

$("<label class='ash'>Comment Removed</label>").html()
   unescape($(txt)).html()

May be it is simple, but can't figure it out

Share Improve this question edited Sep 6, 2012 at 21:24 animuson 54.7k28 gold badges142 silver badges150 bronze badges asked May 13, 2011 at 22:38 kheyakheya 7,62120 gold badges79 silver badges114 bronze badges 1
  • This solution might help – Niclas Sahlin Commented May 13, 2011 at 22:44
Add a comment  | 

2 Answers 2

Reset to default 11

This should do the trick for you:

var elemString = $('<div/>').html("&lt;label class='ash'&gt;Comment Removed&lt;/label&gt;").text();

Here's a demo showing it being appended to the body ->

If you need to do this multiple times, you could simplify with a function, like so:

function DecodeHtml(str) {
    return $('<div/>').html(str).text();
}

var encodedStr = "&lt;label class='ash'&gt;Comment Removed&lt;/label&gt;";
$('body').append(DecodeHtml(encodedStr));

jQuery :

var YuorHtml = "<p>Some Text  <em>Some Text</em> <strong>Some Text</strong></p>";
  
 $('#YuorID').html(YuorHtml)
发布评论

评论列表(0)

  1. 暂无评论