toDataURL不是函数 [英] toDataURL not a function
问题描述
我正在尝试为画布生成一个URL。我遵循的步骤如下:
I am trying to generate a url for the canvas. Here are the steps I followed:
var can = document.getElementsByTagName("canvas");
var src = can.toDataURL("image/png");
当我尝试在萤火虫上运行上述代码时,会引发错误:
When I tried running the above code on firebug it throws an error :
TypeError: can.toDataURL is not a function
我正在Ubuntu上运行Firefox 8。
I am running firefox 8 on ubuntu.
推荐答案
getElementsByTagName
返回 NodeList
[docs] ,而不是单个元素。
getElementsByTagName
returns a NodeList
[docs], not a single element.
只需访问列表的第一个元素:
Simply access the first element of the list:
var src = can[0].toDataURL("image/png");
如果要获取每个画布的数据URL,则必须遍历该列表。否则,给画布一个ID并使用 getElementById
检索引用可能更方便。
If you want to get the data URL for each canvas, then you have to iterate over the list. Otherwise, giving the canvas an ID and retrieving the reference with getElementById
might be more convenient.
这篇关于toDataURL不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!