Jquery 附加 Html 而不是布局

标签 jquery html css ajax

我担心的是,我使用 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:

enter image description here

预期输出

textarea_html_DOM_view_expected:

enter image description here

最佳答案

解码实体,然后通过 jQuery 解析 HTML

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

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

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

关于Jquery 附加 Html 而不是布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54474532/

相关文章:

jquery - 如何在文本区域上启用 select2

javascript - 从 iframe 中选择具有相同类名的两个 DIV 中的第二个

javascript - 页面加载后如何执行if语句?

css - 如何使用 CSS 在页面中心画一条垂直的虚线?

javascript - TinyMCE 隐藏 Div 的占位符

javascript - 尝试连接数字和字符串时出现 NaN Javascript 和 Jquery 错误

html - 在我的浏览器 WAMP 中运行 CGI 脚本

html - SVG .fill 不想工作

html - Zurb 基础框架侧边栏

html - nav div 不会全屏显示