javascript - 未捕获的类型错误 : cannot read property 'querySelectorAll' of null

标签 javascript jquery mobile navigation internet-explorer-11

我正在尝试在网站上使用此移动菜单。 http://tympanus.net/codrops/2013/08/13/multi-level-push-menu/comment-page-8/#comment-466199

我有它的工作,但一个 ie11 用户报告错误,我在控制台中看到以下错误 未捕获的类型错误:无法读取 nullmlPushMenu._init 的属性“querySelectorAll”@mlpushmenu.js:89mlPushMenu @mlpushmenu.js:67(匿名函数)@(索引):1062

这是有问题的 js 文件的片段

function mlPushMenu( el, trigger, options ) {   
    this.el = el;
    this.trigger = trigger;
    this.options = extend( this.defaults, options );
    // support 3d transforms
    this.support = Modernizr.csstransforms3d;
    if( this.support ) {
        this._init();
    }
}

mlPushMenu.prototype = {
    defaults : {
        // overlap: there will be a gap between open levels
        // cover: the open levels will be on top of any previous open level
        type : 'overlap', // overlap || cover
        // space between each overlaped level
        levelSpacing : 40,
        // classname for the element (if any) that when clicked closes the current level
        backClass : 'mp-back'
    },
    _init : function() {
        // if menu is open or not
        this.open = false;
        // level depth
        this.level = 0;
        // the moving wrapper
        this.wrapper = document.getElementById( 'mp-pusher' );
        // the mp-level elements
        this.levels = Array.prototype.slice.call( this.el.querySelectorAll( 'div.mp-level' ) );
        // save the depth of each of these mp-level elements
        var self = this;
        this.levels.forEach( function( el, i ) { el.setAttribute( 'data-level', getLevelDepth( el, self.el.id, 'mp-level' ) ); } );
        // the menu items
        this.menuItems = Array.prototype.slice.call( this.el.querySelectorAll( 'li' ) );
        // if type == "cover" these will serve as hooks to move back to the previous level
        this.levelBack = Array.prototype.slice.call( this.el.querySelectorAll( '.' + this.options.backClass ) );
        // event type (if mobile use touch events)
        this.eventtype = mobilecheck() ? 'touchstart' : 'click';
        // add the class mp-overlap or mp-cover to the main element depending on options.type
        classie.add( this.el, 'mp-' + this.options.type );
        // initialize / bind the necessary events
        this._initEvents();
    },

特定的第 89 行在中间,所以在这里将它拉出来以供您定位

this.levels = Array.prototype.slice.call( this.el.querySelectorAll( 'div.mp-level' ) );

然后我在我的页脚中调用了插件的实例(那是索引行 1082

那个电话看起来像这样

<script>
    new mlPushMenu(
        document.getElementById( 'mp-menu' ),
        document.getElementById( 'trigger' ),
        { type : 'cover' }
    );
</script>

最佳答案

您的桌面站点没有 ID 为“mp-menu”的元素。当您调用 getElementById 方法时,您将得到 null 作为响应。然后将其分配给对象的 el 属性。当您尝试调用 querySelectorAll 时,您是在尝试从空值调用。

根据对上述问题的评论,mp-menu 元素仅出现在移动网站 上。如果是这种情况,此代码也应该仅在移动设备上加载

关于javascript - 未捕获的类型错误 : cannot read property 'querySelectorAll' of null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30656503/

相关文章:

javascript - 减少重复点击事件

javascript - ngrx 使用调度时从列表中删除项目

javascript - dburles meteor Google map API 错误 : MissingKeyMapError

javascript - 简单的抽搐状态

android - 移动设备上显示的网页发生了变化和压缩(与桌面浏览器的区别)

c# - 在 Xamarin PCL 项目中,如何在不单击新字段保留键盘的情况下进入上一个字段后将焦点移至下一个字段?

javascript - 我的 jquery 验证注册表中的一些问题

javascript - jquery ui可与angularjs排序,如何防止水平拖放?轴 :y not working

javascript - 如何在根页面上突出显示索引导航链接

css - 媒体查询在浏览器上看起来与移动设备有很大不同