gpt4 book ai didi

javascript - 图形未显示

转载 作者:太空宇宙 更新时间:2023-11-04 14:43:38 24 4
gpt4 key购买 nike

我只是想使用 Javascript 库 Raphael 创建一些简单的矢量图形。应该有一个方形对象和一个 flex 对象,但没有显示任何内容。谁能帮我。谢谢。

<html>

<head>
<script src="raphael.js"></script>
<script src="jquery-1.7.2.js"></script>

<script type="text/javascript"> //all your javascript goes here
var paper = Raphael("sample-2", 200, 100);
var rectPath = paper.path("M10,10L10,90L90,90L90,10Z");
var curvePath = paper.path("M110,10s55,25 40,80Z");

rectPath.attr({fill:"green"});
curvePath.attr({fill:"blue"});
</script>

</head>

<body>

<div id="sample-2" style="width:500px; height:500px;">

</div>

</body>

</html>

最佳答案

将脚本移动到 <div id="sample-2" style="width:500px; height:500px;"> 之后标签

或者有些人更喜欢使用 onload 处理程序,为简单起见使用 jQuery

$(function(){
// Your code that runs after the DOM is loaded
});

关键是您的代码正在访问 DOM,并且需要在构建 DOM 之后运行。从 onload 处理程序或在您使用的 DIV 之后调用它可确保该元素已准备好与之交互。

关于javascript - 图形未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11529750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com