Javascript 代码仅在 IE 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10970730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript code doesn't work in IE only
提问by haunted85
I have just verified my javascript code works fine in all browsers except IE. How is it possible that the script is correctly read and executed in Chrome, Safari... but IE finds some inexplicable error and refuses to run the code?
我刚刚验证了我的 javascript 代码在除 IE 之外的所有浏览器中都可以正常工作。脚本怎么可能在Chrome、Safari中被正确读取和执行……但IE发现一些莫名其妙的错误并拒绝运行代码?
$(document).ready(function() {
var NewsNavigator = {
init: function(config) {
this.news = config.news;
this.newsNum = this.news.length;
this.navbuttons = config.navbuttons;
this.prevButton = config.prevButton;
this.nextButton = config.nextButton;
this.displayatonce = config.displayatonce;
this.maxSteps = Math.ceil(this.newsNum / this.displayatonce);
this.counter = 0;
this.navigateNews();
this.enableNav();
this.checkButtonsVisibility();
},
showNews: function() {
var start = this.counter * this.displayatonce;
var end = this.counter * this.displayatonce + (this.displayatonce - 1);
for (i=start; i<=end; i++) {
this.news.eq(i).show();
}
},
hideAllNews: function() {
console.log("hiding news");
this.news.hide();
},
navigateNews: function() {
this.hideAllNews();
this.showNews();
},
checkButtonsVisibility: function() {
if (this.counter <= 0)
{
this.prevButton.css('visibility', 'hidden');
}
else
{
this.prevButton.css('visibility', 'visible');
}
if (this.counter >= this.maxSteps - 1)
{
this.nextButton.css('visibility', 'hidden');
}
else
{
this.nextButton.css('visibility', 'visible');
}
},
enableNav: function() {
self = this;
this.navbuttons.on('click', function(event) {
if (($(this).data('dir') === 'prev') && (self.counter > 0)) {
self.counter--;
self.navigateNews();
} else if (($(this).data('dir') === 'next') && (self.counter < self.maxSteps - 1)) {
self.counter++;
self.navigateNews();
}
self.checkButtonsVisibility();
event.preventDefault();
});
}
};
NewsNavigator.init({
news: $('div#cat-news').find('div.oneCol'),
displayatonce: 3,
navbuttons: $('div#nav').find('a'),
prevButton: $('div#nav a.prec'),
nextButton: $('div#nav a.succ')
});
});
Error message in IE9
IE9 中的错误信息
SCRIPT438: The object doesn't support the 'checkButtonsVisibility' property or method.
NewsNavigator.js, Row 69 Character 5
回答by Lai Xin Chu
Well this comes down to the history of JavaScript.
好吧,这归结为 JavaScript 的历史。
JavaScript was implemented based on ECMAScript:
JavaScript 是基于 ECMAScript 实现的:
http://en.wikipedia.org/wiki/ECMAScript
http://en.wikipedia.org/wiki/ECMAScript
Each and every single Web Browser provider (Mozilla, Google, Microsoft) decided that they didn't want to standardize JavaScript and they each came up with their own implementation of the ECMAScript, and thus their own JavaScript engine.
每一个 Web 浏览器提供商(Mozilla、Google、Microsoft)都决定不希望标准化 JavaScript,因此他们各自提出了自己的 ECMAScript 实现,因此也提出了自己的 JavaScript 引擎。
Thus, we programmers get a headache trying to write JavaScript that is compatible across all these different JavaScript engines because each of them read JavaScript in their own way (which answers your question of why IE finds some inexplicable error while the rest doesn't)
因此,我们程序员在尝试编写兼容所有这些不同 JavaScript 引擎的 JavaScript 时会头疼,因为每个引擎都以自己的方式读取 JavaScript(这回答了为什么 IE 会发现一些莫名其妙的错误而其余的则没有)
Fun Fact: Only Mozilla's implementation of ECMAScript is actually called "JavaScript".
有趣的事实:只有 Mozilla 的 ECMAScript 实现才被称为“JavaScript”。
You should lookup on how to write JavaScript that is cross-compatible across different JavaScript engines.
您应该查找如何编写跨不同 JavaScript 引擎交叉兼容的 JavaScript。
回答by Zar
Use a javascript validation tool, such as JSLintto ensure maximum compability. This, since a single omitted character (such as ;
, '
, etc) can cause your script to not be run in specific browsers.
使用 javascript 验证工具,例如JSLint,以确保最大的兼容性。这一点,因为省略的字符(如;
,'
等)可能会导致你的脚本没有特定的浏览器上运行。
JSLint will also provide different tips on what do to and not to do to provide even further compability.
JSLint 还将提供关于该做什么和不该做什么的不同提示,以提供进一步的兼容性。