我遇到了同样的问题,结果证明是加载顺序问题。这是原始代码:
<!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>