dqw7121 2018-01-30 13:54
浏览 50
已采纳

jquery内容不在html表中显示

In my php page, I'm querying a database table and the results are shown in an html table format. One field of the database table contains <script> codes and commented lines along with other code. For eg:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
  <td>
    <!-- STORES html -->

<!-- Start:  Code -->
<script type="text/javascript">
console.log("testing");

if ( $.inArray( 'apple', ["apple", "orange", "grapes"]) > -1 ) { // fruits
    console.log("found");
} // fruits
</script>
<!-- End: -->
  </td>
  <td>row2</td>
</tr>

</table>

The data for the <td> which contains jquery content is not getting displayed. I've added the code in this jsfiddle.

Can anyone help me to fix this? Thanks in advance.

</div>
  • 写回答

2条回答 默认 最新

  • douwen3836 2018-01-30 14:36
    关注

    If you want to display the code, rather than execute it, then you need to HTML-encode it. Whatever language (PHP or JS, I guess) you're using to fetch the content and display it on the page will provide a function to do it. Essentially this involves replacing < with &lt; and > with &gt;.

    The end result should be like this:

    td {
      border: 1px solid black;
      padding: 5px;
    }
    
    table {
      border-collapse: collapse;
    }
    <table>
      <tr>
        <td>
          &lt;!-- STORES html --&gt; &lt;!-- Start: Code --&gt; &lt;script type="text/javascript"&gt; console.log("testing"); if ( $.inArray( 'apple', ["apple", "orange", "grapes"]) > -1 ) { // fruits console.log("found"); } // fruits &lt;/script&gt; &lt;!-- End:
          --&gt;
        </td>
        <td>row2</td>
      </tr>
    
    </table>

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

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line