JavaScript not working as expected

六眼飞鱼酱① 提交于 2019-12-14 03:26:52

问题


I want to draw chart that is beautiful in my VC++ program and so decided to implement in by HTML and JavaScript.

As it show, I downloaded a Javascript lib and used it as it said in its site.
How to use it?

Download and include raphael.js into your HTML page, then use it as simple as:

// Creates canvas 320 × 200 at 10, 50
var paper = Raphael(10, 50, 320, 200);

// Creates circle at x = 50, y = 40, with radius 10
var circle = paper.circle(50, 40, 10);

// Sets the fill attribute of the circle to red (#f00)
circle.attr("fill", "#f00");

// Sets the stroke attribute of the circle to white    
circle.attr("stroke", "#fff");

I included javascript file like below:

<head>
<script type="text/javascript" src="raphael.js"></script>
<script type="text/javascript">
ABOVE CODE
</script>

If you try it, it won't work. I tried to make it work, but could not. I have a question. How can I do it work without copying things from save as page of their demo? Of course, it includes documentation, but it is reference.

How does it work simply? Thanks!


回答1:


Check the reference to the Raphael library, and then try this code:

<head>
  <script type='text/javascript' src='raphael.js'></script>

  <script type='text/javascript'>
  //<![CDATA[ 
  window.onload=function(){
    // Creates canvas 320 × 200 at 10, 50
    var paper = Raphael(10, 50, 320, 200);

    // Creates circle at x = 50, y = 40, with radius 10
    var circle = paper.circle(50, 40, 10);
    // Sets the fill attribute of the circle to red (#f00)
    circle.attr("fill", "#f00");

    // Sets the stroke attribute of the circle to white    
    circle.attr("stroke", "#fff");
  }
  //]]> 
  </script>
</head>

This works for me, make sure your reference is good.

JSFiddle




回答2:


  • Is Raphael linked properly? (view source then press the raphael.js link - does it work?)
  • Try to use examples from the reference. Your code should paint across the entire screen.
  • Do you see a new element created in the page? use firebug for this.
  • Which version of Raphael are you using? Recently it was upgraded to version 2.


来源:https://stackoverflow.com/questions/7702241/javascript-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!