【问题标题】:Jquery Appending Html Instead of LayoutJquery附加Html而不是布局
【发布时间】:2019-02-01 16:45:04
【问题描述】:

我担心的是,我正在使用 ajax 从数据库中获取原始 HTML 作为字符串。一切就绪。

但每当我将它附加到 DIV 中时,它都会将整个代码按原样放置,而不是将其转换为布局格式。

代码如下:

JQuery

var str='&lt;textarea rows="4" cols="10" class="form-control physical_examination(urine)_field1" name="physical_examination(urine)_field1[]" placeholder=""&gt;&lt;/textarea><br/>&lt;textarea rows="4" cols="10" class="form-control physical_examination(urine)_field2" name="physical_examination(urine)_field2[]" placeholder=""&gt;&lt;/textarea><br/>&lt;textarea rows="4" cols="10" class="form-control physical_examination(urine)_field3" name="physical_examination(urine)_field3[]" placeholder=""&gt;&lt;/textarea><br/>&lt;textarea rows="4" cols="10" class="form-control physical_examination(urine)_field4" name="physical_examination(urine)_field4[]" placeholder=""&gt;&lt;/textarea><br/>&lt;textarea rows="4" cols="10" class="form-control physical_examination(urine)_field5" name="physical_examination(urine)_field5[]" placeholder=""&gt;&lt;/textarea><br/>';

var generatedView=$(".generatedView");

generatedView.append(str);

HTML

<div class="generatedView"></div>

输出

textarea_html_view_presented:

预期输出

textarea_html_DOM_view_expected:

【问题讨论】:

  • <到
  • 可以用jquery解析html
  • 如果它包含 &amp;lt;&amp;gt;,那么它实际上不是“原始 HTML”……问题是,为什么不是。你从服务器发送一个“编码”版本,只需要在客户端再次“解码”它,这似乎没有什么意义。那么为什么这不发送 正确 “原始” HTML 开始呢?

标签: jquery html css ajax


【解决方案1】:

解码实体然后解析 HTML qith jQuery

const input  = '.... see question ....',
      html   = input
          .replace(/&lt;/g, '<')
          .replace(/&gt;/g, '>'),
      parsed = $.parseHTML(html);

$(".generatedView").append(parsed);

注意:这是一种简单的方法。如果您的输入中有其他实体,您应该查看一个真实的实体解码模块。

【讨论】:

  • 它只是替换第一次出现的 <或 >
  • 抱歉,开发人员在键盘上睡着了 :-) 已修复。
  • 如果您对它感到满意,请记住接受关闭问题的答案。
猜你喜欢
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 2019-02-10
相关资源
最近更新 更多