Javascript适用于FF和IE ..但Chrome说“不能调用null的方法”!

时间:2010-09-11 21:26:21

标签: javascript google-chrome

包含错误的代码是:

var Slide = new Class({
    initialize: function(triggers, panels) {
        this.triggers = $(triggers).getElements('a[rel=content1-1]');
        this.panels = $(panels).getElements('ul[class=rel-content1-1]');
        this.active = -1;
        this.toggle();
    }, ...
})

稍后会在同一个文件中调用它:

function activateSliders() {
    var slide_1 = new Slide('aCol', 'content');
    var slide_2 = new SlideTwo('content', 'content2', 'content2-hider');
}
window.onload = activateSliders();

为什么Chrome(仅限Chrome)将$(triggers)计算为NULL?

1 个答案:

答案 0 :(得分:3)

根据我的经验,IE和FF倾向于偶尔慷慨地让jQuery代码很好地工作而不用将其封装在$(document).ready(块中。尝试:

$(document).ready(function() {
    activateSliders();
});