jquery - how to render html using javascript - Stack Overflow

I have a javascript engine(using jquery) that needs to render a html which es as a output from a third

I have a javascript engine(using jquery) that needs to render a html which es as a output from a third party server (REST API). The response contains plain text and occasional html elements like <b>, <p> <br> etc which I want to render as html output. (and not literally as <b>, <p> etc)

Is there a way ?

Here is what I am doing - in pseudo code. Note : I am using blueimp javascript template to generate code.

jQuery.get({
    url: 'someRESTfulURL/id',
    method: 'get',
    success: function(resp) {
      //resp contains html elements like <b> etc
      var data = {title: resp.title, content: resp.content};
      $("#maindiv").html(tmpl("text-tmpl", data)); 
    }
});

<script type="text/x-tmpl" id="text-tmpl">
    <h3>{%=o.title%}</h3>
    <p>{%=o.content%}</p>
</script>

<html><body><div id='maindiv'></div></body></html>

The javascript template is encoding the html characters and hence the problem. Is there a way I can use this template and still render the html chars.

I have a javascript engine(using jquery) that needs to render a html which es as a output from a third party server (REST API). The response contains plain text and occasional html elements like <b>, <p> <br> etc which I want to render as html output. (and not literally as <b>, <p> etc)

Is there a way ?

Here is what I am doing - in pseudo code. Note : I am using blueimp javascript template to generate code.

jQuery.get({
    url: 'someRESTfulURL/id',
    method: 'get',
    success: function(resp) {
      //resp contains html elements like <b> etc
      var data = {title: resp.title, content: resp.content};
      $("#maindiv").html(tmpl("text-tmpl", data)); 
    }
});

<script type="text/x-tmpl" id="text-tmpl">
    <h3>{%=o.title%}</h3>
    <p>{%=o.content%}</p>
</script>

<html><body><div id='maindiv'></div></body></html>

The javascript template is encoding the html characters and hence the problem. Is there a way I can use this template and still render the html chars.

Share Improve this question edited Jan 29, 2014 at 12:27 Supra asked Jan 29, 2014 at 0:41 SupraSupra 1,6421 gold badge18 silver badges36 bronze badges 4
  • please share your code – Arun P Johny Commented Jan 29, 2014 at 0:42
  • 5 Have you tried $('#yourelement').html(data); ?? – dev7 Commented Jan 29, 2014 at 0:43
  • Yes, I have. BUt I think the problem lies somewhere, as far as I can tell after digging. Probably lies with the template I am using. – Supra Commented Jan 29, 2014 at 12:21
  • 1 @Dilpa If you still got that issue, you should have a look at the answer I gave. It addresses the JS templating you are doing, whereas the other hints didn't really take the template engine you are using into account. – bouscher Commented Jan 31, 2014 at 10:44
Add a ment  | 

2 Answers 2

Reset to default 3

There are two ways you can do this with jQuery:

  1. The first is when you have the response data already, then you can put it into an element like this:

    $("#myElement").html(myData);
    
  2. The second is that you could load the data directly into the element:

    $("#myElement").load("http://myurlgoeshere./webservice");
    

You have to prevent the escaping of HTML special characters. Try this:

<script type="text/x-tmpl" id="text-tmpl">
    <h3>{%#o.title%}</h3>
    <p>{%#o.content%}</p>
</script>

The difference is just the '#' instead of the '='.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745467438a4628977.html

相关推荐

  • jquery - how to render html using javascript - Stack Overflow

    I have a javascript engine(using jquery) that needs to render a html which es as a output from a third

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信