简体   繁体   English

在表格的 td 中添加 html 字符串

[英]Add html string inside td of table

I need create a table from array string, and I use template script, it working with normal string but when I add string have html tag (ex: test) in array and my result show a button in td tag but I want it is string "test".我需要从数组字符串创建一个表,我使用模板脚本,它使用普通字符串,但是当我添加字符串时,数组中有 html 标记(例如:test)并且我的结果在 td 标记中显示一个按钮,但我希​​望它是字符串“测试”。 How I can show string html in td?如何在 td 中显示字符串 html? Here is my code这是我的代码

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JSON Transform</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script> <script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script> <script type="text/template" id="tpl-html1"> <div class="well"> <table class="table"> <tbody> <% _.each( target, function(i) {%> <tr> <td>'<%= i %>'</td> </tr> <% }); %> </tbody> </table> </div> </script> <script> $( document ).ready(function() { var rawData = ["line 1 <button>test</button>","line 2"," line3"]; var data = { target:rawData }; var template = _.template( $("#tpl-html1").text() ); $("#output").append( template(data) ); }) </script> </head> <body style="padding:50px 10px "> <div id="output"></div> </body> </html>

Thank you verymuch.非常感谢。

If you want to display the without it converting to an HTML element you can escape the > and < symbols like this:如果您想在不将其转换为 HTML 元素的情况下显示它,您可以像这样转义 > 和 < 符号:

'<' = '&lt;'
'>' = '&gt;'

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>JSON Transform</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script> <script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script> <script type="text/template" id="tpl-html1"> <div class="well"> <table class="table"> <tbody> <% _.each( target, function(i) {%> <tr> <td>'<%= i %>'</td> </tr> <% }); %> </tbody> </table> </div> </script> <script> $( document ).ready(function() { var rawData = ["line 1 &lt;button&gt;test&lt;/button&gt;","line 2"," line3"]; var data = { target:rawData }; var template = _.template( $("#tpl-html1").text() ); $("#output").append( template(data) ); }) </script> </head> <body style="padding:50px 10px "> <div id="output"></div> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM