开发者

Jquery - Convert ascii chars back to HTML

I am reading data from an html file to load into a div. The problem I am having is, the program that writes the html files is converting <br /> to &lt;br /&gt;

SO when I execute

$('#items').load('/News/list.aspx');

it displays <br />开发者_高级运维 as a string on my page instead of reading it as a page break.

I have tried to read the above file into a variable to do a string replace on the &lt;br /&gt; but it doesn't seem to work.

Any suggestions?


First step would be modifying the server-side script if possible so that the HTML doesn't get encoded in the first place.

Failing that, you can use the ajax method to load a page's data into a string first. The method you're currently using just loads the text immediately.

$.ajax({
    type: 'GET',
    url: '/News/list.aspx',
    dataType: 'text',
    success: function(response) {
        response = response.replace( /&lt;/g, '<' );
        response = response.replace( /&gt;/g, '>' );
        $('#items').html(response);
    }
});

Here I've replaced individual angle brackets, which will convert everything back to HTML. If you only wanted line breaks and nothing else, replace those two lines with response = response.replace( /&lt;br \/&gt;/g, '<br />' );


Is it URLencoding the < and > tags?

Have you tried decoding the string you get back with something like this? :

http://plugins.jquery.com/project/URLEncode

HTH

EDIT: Thats not URL encoding so this won't work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜