【问题标题】:Javascript error - cannot call method 'appendChild' of null (svgicons)Javascript 错误 - 无法调用 null 的方法“appendChild”(svgicons)
【发布时间】:2015-07-07 18:09:33
【问题描述】:

我在 Chrome 开发人员工具中收到此错误,但我在页面上看不到任何错误。

这是 JS 的简短摘录:

function svgIcon( el, config, options ) {
        this.el = el;
        this.options = extend( {}, this.options );
        extend( this.options, options );
        this.svg = Snap( this.options.size.w, this.options.size.h );
        this.svg.attr( 'viewBox', '0 0 64 64' );
        this.el.appendChild( this.svg.node );
        // state
        this.toggled = false;
        // click event (if mobile use touchstart)
        this.clickevent = mobilecheck() ? 'touchstart' : 'click';
        // icons configuration
        this.config = config[ this.el.getAttribute( 'data-icon-name' ) ];
        // reverse?
        if( hasClass( this.el, 'si-icon-reverse' ) ) {
            this.reverse = true;
        }
        if( !this.config ) return;
        var self = this;

我相信这段代码说明不了太多,所以页面可以在这里找到:http://goo.gl/j7M5r8

我已经尝试通过查看 Stackoverflow 上的其他类似案例来解决问题,它指出要么缺少某些东西,要么有某些东西与读取/写入的顺序有关。

我还没有写 svgicons.js,所以应该不会有任何问题。但是我可以看到有些人和我有同样的问题,但没有提供对该问题的解释或答案。 Here is basically the same question from another person.

相关文件:

http://goo.gl/0RisGa

http://goo.gl/UgWHMS

http://goo.gl/X269NO

【问题讨论】:

标签: javascript svg


【解决方案1】:

检查您的 hamburger_animations.js 文件 - 它位于 http://www.vekvemedia.no/wp-content/themes/vekvemedia/js/hamburger_animation.js

(function() {                           
    [].slice.call( document.querySelectorAll( '.si-icons-default > .si-icon' ) ).forEach( function( el ) {
        var svgicon = new svgIcon( el, svgIconConfig );
    } );
    new svgIcon( document.querySelector( '.si-icons-easing .si-icon-hamburger' ), svgIconConfig, { easing : mina.backin } );
    new svgIcon( document.querySelector( '.si-icons-easing .si-icon-hamburger-cross' ), svgIconConfig, { easing : mina.elastic, speed: 600              } );
    })();

document.querySelector( '.si-icons-easing .si-icon-hamburger' ) 导致 null

【讨论】:

  • 感谢您的评论。如果我能得到任何不同,我实际上试图立即删除该行,但不,仍然错误......
  • 也删除下一行,你没有类 si-icon-easing 的元素,所以这两个 querySelector 行都会失败
  • 好的,谢谢。令人印象深刻的是,您解决问题的速度有多快。再次谢谢你:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多