11

我正在尝试将 raphael.js(下载并在本地运行)加载到 HTML 文件中,但脚本拒绝退出,在我的 JS 控制台中出现错误:

Uncaught TypeError: 
Cannot call method 'appendChild' of null
bV                   on raphael.js:7
a                    on raphael.js:7
(anonymous function) on raphael.html:22

这是针对缩小版的,在第 1789 行的非最小版中也会出现同样的错误。

我从网站下载了代码,尝试了压缩和未压缩,以及下载了其中一个演示中链接的 JS 文件,所有这些都可以在我的浏览器(chrome)中正常工作。

有什么想法吗?

4

1 回答 1

28

我遇到了同样的问题,结果证明是加载顺序问题。这是原始代码:

<!doctype html>
<html>
  <head>
    <script src='raphael-1.5.2.js'></script>
    <script>
        var paper = Raphael(10, 50, 300, 250);
        var circle = paper.circle(50, 40, 10);
        circle.attr('fill', '#c00');
        circle.attr('stroke', '#fff');
    </script>
  </head>

  <body></body>
</html>

事实证明,当Raphael(10, 50, 300, 250)被调用时,它会尝试将一个画布元素附加到 body ......这还不存在。所以将它包装在一个window.onload或 jQuery onload 函数中解决了这个问题:

<!doctype html>
<html>
  <head>
    <script src='raphael-1.5.2.js'></script>
    <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js'></script>
    <script>
      $(function () {
        var paper = Raphael(10, 50, 300, 250);
        var circle = paper.circle(50, 40, 10);
        circle.attr('fill', '#c00');
        circle.attr('stroke', '#fff');
      });
    </script>
  </head>

  <body></body>
</html>
于 2010-12-22T17:41:46.370 回答