javascript - 如何让 HTML 运行而不是从数据库显示 html 命令?

标签 javascript php html nicedit

我正在使用 nicEdit.js,它会像这样保存到数据库:

<div align="center"><i>test</i><br></div><u><br><font size="5"><b>Become part of our team!</b></font><br></u>

我从数据库中将其检索为 $jobfulldesc。

这将正确显示:

<?php
function unhtmlentities ($string) {
  $trans_tbl =get_html_translation_table (HTML_ENTITIES );
  $trans_tbl =array_flip ($trans_tbl );
  return strtr ($string ,$trans_tbl );
}
echo unhtmlentities($jobfulldesc); ?>

如果您尝试将 nicEdit 添加到 JavaScript 中,这将不起作用:

<script type="text/javascript">
  bkLib.onDomLoaded(function() {
      var myNicEditor = new nicEditor();
      myNicEditor.setPanel('myInstance1');

 });
</script>
<div id="myInstance1"><?php echo unhtmlentities($jobfulldesc); ?></div>

或内部:

<textarea><?php echo unhtmlentities($jobfulldesc); ?></textarea>

如何使其在没有 nicEdit 内的 HTML 格式的情况下从数据库中可见?

最佳答案

您只能使用 Javascript 解析 HTML 中的文本:

function getText(htmlString) {
       const virtual = document.createElement('div');
       virtual.innerHTML = htmlString;
       return virtual.textContent
}

假设htmlString来自数据库并通过PHP传递给javascript。

演示:

function getText(htmlString) {
       const virtual = document.createElement('div');
       virtual.innerHTML = htmlString;
       return virtual.textContent
}

const retreivedHtml = `<div align="center"><i>test</i><br></div><u><br><font size="5"><b>Become part of our team!</b></font><br></u>`;

console.log(
   getText(retreivedHtml)
)
// inject it in the textarea
document.querySelector('textarea').value= getText(retreivedHtml)
 <textarea></textarea>

关于javascript - 如何让 HTML 运行而不是从数据库显示 html 命令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42196088/

相关文章:

javascript - NightmareJS 多重评估

php - PHP 可以使用 MySQL 书签查询吗?

javascript - fabric.js - 重新调整尺寸后获得新高度

用于验证字母的 Javascript

javascript - 检查视频是否正在流式传输

javascript - 如果加载时已选中复选框

javascript正则表达式在第一个等号处分隔字符串

php - 我怎样才能建立统计 map ?

javascript - 为什么 hasOwnProperty 无法识别对象原型(prototype)上的函数?

PHP PDO更新错误: number of bound variables does not match number of tokens