Tab*_*rez 1 javascript jquery internet-explorer ruby-on-rails cross-browser
我在document.ready函数中有以下语句:
if($("sidebar ").html().trim().length == 0)
{
$("sidebar").append("<p> The sides..</p>");
};
Run Code Online (Sandbox Code Playgroud)
它在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
Run Code Online (Sandbox Code Playgroud)
我在调试模式下查看了.js,看到上面的语句转换为:
$("sidebar ").html().trim().length==0&&$("sidebar").append("<p> The sides..</p>")
Run Code Online (Sandbox Code Playgroud)
如何防止此错误?请注意,我确实看到该节点存在于呈现的页面中.
我认为可能只是提到shiv5.html可能不足以照顾IE的特性.所以,我通过sprockets添加了modernizr.js,并在我的布局中添加了class ="no-js".在IE <9中仍然没有运气.
我错过了什么?我还能做些什么来获得微软霸主的青睐?
根据MDN,trim在IE <9中不可用.
你可以$.trim改用:
if($.trim($("sidebar ").html()).length == 0)
{
$("sidebar").append("<p> The sides..</p>");
} // <-- Don't want a semicolon here.
Run Code Online (Sandbox Code Playgroud)
如果您不想查找所有实例trim并更正它们,MDN文章会列出一个替代方案..trim如果它不是本机可用的,您可以使用以下内容创建:
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
};
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1277 次 |
| 最近记录: |