jquery .html() how to render twice
I am passing a php string which contains html special characters, such as <div class="myclass
...
When I pass it to .html() it render it to actual html code but doesnt format it. the example above will be
"div class = "myclass"
Looks like I need to render the content again .html but i am getting a blank page. This is my code
var htmlStr = $().html(Myobject开发者_开发知识库.variable);
$("div.container").html(htmlStr);
Give this a try...
var post_html = '<div class="myclass" > stuff </div>';
var html = post_html.replace(/&(lt|gt|quot);/g, function($0){
switch($0){
case '<': return '<';
break;
case '>': return '>';
break;
case '"': return '"';
break;
}
});
Example: http://jsfiddle.net/SUXkm/
Ideally I would try and HTML encode the content on the server before sending it to the client. Unfortunately JavaScript does not have a built in HTML encode/decode function but there is a library that someone has written that attempts to do this:
http://www.strictly-software.com/htmlencode
http://www.strictly-software.com/scripts/downloads/encoder.js
精彩评论