[英]Jquery issues on older versions of IE
I have the following statement in document.ready function: 我在document.ready函数中有以下声明:
if($("sidebar ").html().trim().length == 0)
{
$("sidebar").append("<p> The sides..</p>");
};
It works fine in IE 9 but as soon as I select IE 8 (browser and document standard), the script stops working and gives the following error: 它在IE 9中可以正常工作,但是一旦我选择IE 8(浏览器和文档标准),脚本就会停止工作并给出以下错误:
SCRIPT5007: Unable to get value of the property 'trim': object is null or undefined
application-e51c9eee7d22e9644481115dc1fedd5f.js, line 7 character 17578
I looked at the .js in debug mode and see that my statement above is transformed to: 我在调试模式下查看了.js,发现上面的语句转换为:
$("sidebar ").html().trim().length==0&&$("sidebar").append("<p> The sides..</p>")
How do I prevent this error? 如何防止此错误? Please note that I do see that the node is present in the rendered page. 请注意,我确实看到了该节点存在于渲染页面中。
I thought that maybe just having reference to shiv5.html may not be sufficient to take care of the IE idiosyncrasies. 我认为,仅引用shiv5.html可能不足以照顾IE的特质。 So, I have added modernizr.js via sprockets and I have added class="no-js" in my layout. 因此,我通过链轮添加了modernizr.js,并在布局中添加了class =“ no-js”。 Still no luck in IE <9. IE <9仍然没有运气。
What am I missing? 我想念什么? What else can I do to get the favor of Microsoft overlords? 我还能做些什么来获得Microsoft霸主的青睐?
According to MDN, trim
isn't available in IE < 9. 根据MDN,在IE <9中无法使用trim
。
You could use $.trim
instead: 您可以改用$.trim
:
if($.trim($("sidebar ").html()).length == 0)
{
$("sidebar").append("<p> The sides..</p>");
} // <-- Don't want a semicolon here.
The MDN article lists an alternative if you don't want to find all the instances of trim
and correct them. 如果您不想找到所有trim
实例并对其进行更正,则MDN文章列出了一种替代方法。 You could use the following to create .trim
if it's not natively available: 如果.trim
本机不可用,则可以使用以下命令创建它:
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
};
}
Check out this thread. 签出此线程。 After a quick search it seems that many people are experiencing issues with trim. 快速搜索后,似乎很多人都遇到了修剪问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.