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 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了