jQuery +客户端模板="语法错误,无法识别的表达式" [英] jQuery + client-side template = "Syntax error, unrecognized expression"
问题描述
我刚刚将jQuery从1.8.3更新到1.9,它突然崩溃了。
I just updated jQuery from 1.8.3 to 1.9, and it started crashing all of a sudden.
这是我的模板:
<script type="text/template" id="modal_template">
<div>hello</div>
</script>
这是我读它的方式:
modal_template_html = $("#modal_template").html();
这是我将它转换为jQuery对象的方式(我需要在它上面使用jQuery方法):
This is how I transform it into jQuery object (I need to use jQuery methods on it):
template = $(modal_template_html);
...和jQuery崩溃!
... and jQuery crashes!
错误:语法错误,无法识别的表达式:< div> hello< / div>
Error: Syntax error, unrecognized expression: <div>hello</div>
slice.call(docElem.childNodes,0)[0] .nodeType;
slice.call( docElem.childNodes, 0 )[0].nodeType;
jquery-1.9.0.js(第3811行)
jquery-1.9.0.js (line 3811)
但是,如果我将模板声明为纯文本变量,它再次开始工作:
However, if I declare template as a plain text variable, it starts working again:
var modal_template_html = '<div>hello</div>';
任何人都可以帮我解决这个问题吗?
Can anyone help me to figure this out?
更新:
Jquery团队听到并且改变 1.10中的事情恢复正常:
UPDATE: Jquery team heard and changed things back to normal in 1.10:
你可能会看到的最大变化是我们放宽了$()中HTML处理的标准,允许前导空格和换行符,就像我们在版本1.9之前所做的那样
The biggest change you’re likely to see is that we’ve loosened up the criteria for HTML processing in $(), allowing leading spaces and newlines as we did before version 1.9
推荐答案
使用换行符(或<以外的任何内容)开头的字符串不被视为jQuery 1.9中的HTML字符串
Turns out string starting with a newline (or anything other than "<") is not considered HTML string in jQuery 1.9
< a href =http://stage.jquery.com/upgrade-guide/1.9/#jquery-htmlstring-versus-jquery-selectorstring =noreferrer> http://stage.jquery.com/upgrade-guide /1.9/#jquery-htmlstring-versus-jquery-selectorstring
这篇关于jQuery +客户端模板="语法错误,无法识别的表达式"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!