我尝试在jquery模板中使用{{html}}加载字符串。 但脚本注入会影响{{html}}。 如何限制脚本注入。
答案 0 :(得分:0)
问题不在于jQuery-template,而是因为html解析了字符串文字中的<script>
元素。见Why split the <script> tag when writing it with document.write()?
如果要解析并执行脚本,那么一种简单的方法是将脚本移动到单独的文件(并在html页面中包含该文件),而不是内联它。
或者你可以像
一样逃避它
testTemplate = "<i>{{html txt}}</i>";
$.template('testTemplate', testTemplate);
$.tmpl("testTemplate", {
txt: "<b>bold</b> and \x3Cscript>alert('abc')\x3C/script>"
}).appendTo("#target");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.js"></script>
<ul id="target"></ul>