2016-08-25 66 views
0

这是我的代码: 我不认为<head><html><body>零件与我的问题有关。 <head>只包含一个指向CSS文件的链接。 我试图把画布放入我的页面,但由于某种原因没有出现。有人可以看我的代码,看看是否有什么使它不起作用?我想在我的页面上做一个画布,但它不起作用

<div id="title"> 
    <h1 class="main">Options</h1> 
    </div> 
    <div id="field"> 
    <p> 
    <div id="rad"> 
     <input type="radio" name="bookmarks" value="ab" checked="checked"> All bookmarks<br> 
     <input type="radio" name="bookmarks" value="ao"> One folder<br> 
    </div> 
    <input id="input" type="text" value="Bookmark folder name"> 

    <br> 

    <button onClick="setname(null);" id="sub">Submit</button> 
    </p> 
    </div> 
    <canvas id="myCanvas" width="400" height="500"> 
    Your browser does not support the HTML5 canvas tag. 
    </canvas> 

    <script> 
    var c = document.getElementById("myCanvas"); 
    var ctx = c.getContext("2d"); 
    ctx.fillStyle = "#000000"; 
    ctx.fillRect(9,9,9,9); 
    </script> 

截图:在画布上已经完全加载之前 imgur

+0

你应该*不*换'div'内'p'标签 –

回答

1

你的脚本代码执行。

使用window.onload等待画布负载:

<script> 
    window.onload=(function(){ 
     var c = document.getElementById("myCanvas"); 
     var ctx = c.getContext("2d"); 
     ctx.fillStyle = "#000000"; 
     ctx.fillRect(9,9,9,9); 
    }); 
</script> 
+1

谢谢!我忘了那个:P –

相关问题