dongya5893 2016-08-11 13:36 采纳率: 100%
浏览 283
已采纳

将HTML转换为有效字符串

I currently have a PHP project that uses the Zend Framework. When a user clicks a button, I am using a partial to get the HTML that needs to be added to the page. The problem is that the HTML seems to be returned as HTML rather than a string:

document.getElementById("active_projects").innerHTML='<?php echo $this->partial("project/project/activeprojectstable.phtml", array(
    "headers" => $headers, 
    "active" => $this->active

))?>';

.innerHTML = takes a string, but the HTML has new lines and special characters that it does not accept. How can I convert the HTML to a valid HTML string?


Update:

For a better idea of the HTML being produced by the partial that is causing the issues, this is what it evaluates to:

error from html

  • 写回答

2条回答 默认 最新

  • drrhr20884 2016-08-11 14:12
    关注

    Try encapsulating your partial into a script tag with type="text/template". Browser will not understand what type of script it is, therefore it will not execute it. Nevertheless, you can still access its contents, even if they are multiline.

    <script type="text/template" id="active_projects_template">
      <?php echo $this->partial("project/project/activeprojectstable.phtml", array(
        "headers" => $headers, 
        "active" => $this->active
      ))?>
    </script>
    
    <script type="text/javascript">
      // ...
      document.getElementById("active_projects").innerHTML=document.getElementById("active_projects_template").innerHTML;
      // ...
    </script>
    

    Similar pattern is being used by templates in AngularJS (type is set to text/ng-template).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错