2016-07-29 128 views
-1

这有什么问题?我得到一个imText没有定义的错误。我做了一些搜索,发现它与范围有关?有没有更好的方法来实现我想要做的事情,因为它也出现使用onClick事件也不建议。JS函数未定义(ReferenceError)

<script> 
    function imText(text) { 
    var c = document.getElementById("canvas"); 
    var ctx = c.getContext("2d"); 
    ctx.font="20px Georgia"; 
    ctx.fillStyle="white"; 
    ctx.fillText(text, 10, 50); 
{ 
</script> 

<div class="canvas"></div> 
    <canvas id="area"></canvas> 
    <form action="text"> 
    <h2>Enter Text:</h2> 
    <input type="text" id="getText" name="text" value="bee"/><br> 

    <input type="button" value="Submit" onclick="imText(document.getElementById('getText').value)" /> 
    </form> 
+1

脚本中的最后一个字符('{')应该是'}' – Iso

+1

您似乎没有关闭您的'imText'函数。它看起来像一个简单的错字,用'}替换最后一个'''' – NewToJS

回答

0

也许这个右括号:

<script> 
    function imText(text) { 
    var c = document.getElementById("canvas"); 
    var ctx = c.getContext("2d"); 
    ctx.font="20px Georgia"; 
    ctx.fillStyle="white"; 
    ctx.fillText(text, 10, 50); 
} 
</script> 
0

没什么大不了的,一对夫妇的错误:</script>前收盘支架和getElementById应引用“区域”而不是“帆布”,因为你给canvas元素的ID为 “区域”

<script> 
    function imText(text) { 
    var c = document.getElementById("area"); 
    var ctx = c.getContext("2d"); 
    ctx.font="20px Georgia"; 
    ctx.fillStyle="white"; 
    ctx.fillText(text, 10, 50); 
} 
</script> 

<div class="canvas"></div> 
    <canvas id="area"></canvas> 
    <form action="text"> 
    <h2>Enter Text:</h2> 
    <input type="text" id="getText" name="text" value="bee"/><br> 

    <input type="button" value="Submit" onclick="imText(document.getElementById('getText').value)" /> 
    </form> 

作品在此example link

+0

我在你提供的JFiddle链接中添加了一些额外的CSS,所以我可以看到它工作,但它不起作用。在我的浏览器的控制台中,它现在说“TypeError:无法设置null的属性'字体'”。 –

+0

不确定哪些浏览器支持这些功能..我在Chrome上,它工作正常。它可能是你的浏览器。 – evilpenguin

+0

我也在使用chrome。 –

相关问题