dongqiang1894 2017-02-13 03:11
浏览 38
已采纳

如何从数据库中运行HTML而不是show html命令?

I am using nicEdit.js and it saves to the database like this:

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

From the database I retrieve it as $jobfulldesc.

This will show it correctly:

<?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); ?>

That just doesn't work if you try to add nicEdit into your javascript:

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

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

or inside of:

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

How do you make it be seen without the format of HTML inside of nicEdit from the database?

  • 写回答

1条回答 默认 最新

  • duanqinjiao5244 2017-02-13 03:16
    关注

    You can parse the text from HTML using Javascript only:

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

    Assuming htmlString is coming from database and passing to javascript through PHP.

    demo:

    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>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示