开发者

jquery .html() how to render twice

开发者 https://www.devze.com 2023-02-15 18:42 出处:网络
I am passing a php string which contains html special characters, such as <div class="myclass ...

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 '&lt;': return '<';
        break;
        case '&gt;': return '>';
        break;
        case '&quot;': 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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号