duanan6043 2015-06-15 08:22
浏览 220
已采纳

字符串无法解析为HTML

I have a weird problem with my PHP and Javascript files. I have HTML content in my Database, and I want to insert that into my page. So, I get the values and pass it to the HTML page like this:

$string = preg_replace( "/|
/", "", htmlentities($formcontent) );

I get a string which I can use to pass it to my javascript, like this:

var contentform = '<?php echo $string; ?>';

Now I want to insert this HTML into my page. I've got a div set up, but if I insert it, it keeps getting treated as a string so there isn't any DOM to display.

var htmltrimmed = $.trim(contentform);

var htmlcontent = $.parseHTML(htmltrimmed);
console.log(htmltrimmed);
$('#currentform').html(htmltrimmed);

But when I check the console of check the actual div, there is just a plain string inserted.:

string in html

You see it gets treated as a string. But I really don't understand why it's acting like this.

EDIT:

This is the contentform value, I've cut some of it off, because the string is very long, but the actual value is a valid HTML string!:

&lt;form class=&quot;form-horizontal&quot; &gt;&lt;fieldset&gt;&lt;!-- Form Name --&gt;&lt;legend&gt;DMN SGT&lt;/legend&gt;&lt;!-- Multiple Radios --&gt;&lt;div class=&quot;control-group&quot;&gt;  &lt;label class=&quot;control-label&quot; for=&quot;multipleradios-0&quot;&gt;Grund der Messebesuch&lt;/label&gt;  &lt;div class=&quot;controls&quot;&gt;    &lt;label class=&quot;radio&quot; for=&quot;multipleradios-0-0&quot;&gt;      &lt;input type=&quot;radio&quot; name=&quot;multipleradios-0&quot; id=&quot;multipleradios-0-0&quot; value=&quot;Konkretes Interesse an Produkt&quot; checked=&quot;checked&quot;&gt;
  • 写回答

3条回答 默认 最新

  • douyuan1752 2015-06-15 08:39
    关注

    Use $.parseHTML for your example

    see this Example: http://jsfiddle.net/kevalbhatt18/Lny2m0kc/1/

    var t = $.parseHTML('&lt;form class=&quot;form-horizontal&quot; &gt;&lt;fieldset&gt;&lt;!-- Form Name --&gt;&lt;legend&gt;DMN SGT&lt;/legend&gt;&lt;!-- Multiple Radios --&gt;&lt;div class=&quot;control-group&quot;&gt;  &lt;label class=&quot;control-label&quot; for=&quot;multipleradios-0&quot;&gt;Grund der Messebesuch&lt;/label&gt;  &lt;div class=&quot;controls&quot;&gt;    &lt;label class=&quot;radio&quot; for=&quot;multipleradios-0-0&quot;&gt;      &lt;input type=&quot;radio&quot; name=&quot;multipleradios-0&quot; id=&quot;multipleradios-0-0&quot; value=&quot;Konkretes Interesse an Produkt&quot; checked=&quot;checked&quot;&gt;')
    console.log(t);
    
    $('#test').html(t[0].data)
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示