【问题标题】:Can't load Raphael.js library, appendChild method not found?无法加载 Raphael.js 库,找不到 appendChild 方法?
【发布时间】:2011-05-05 06:59:59
【问题描述】:

我正在尝试将 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)中运行良好。

有什么想法吗?

【问题讨论】:

标签: javascript raphael appendchild


【解决方案1】:

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

<!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) 被调用时,它会尝试将一个canvas 元素附加到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>

【讨论】:

  • 2013 这仍然是相关的。谢谢!
  • 2014年6月,这一次的力道依然强劲
猜你喜欢
  • 1970-01-01
  • 2018-03-14
  • 2015-02-04
  • 2019-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
相关资源
最近更新 更多