我刚刚验证了我的 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')
});
});
IE9 中的错误信息
SCRIPT438: The object doesn't support the 'checkButtonsVisibility' property or method.
NewsNavigator.js, Row 69 Character 5