toDataURL不是函数 [英] toDataURL not a function

查看:98
本文介绍了toDataURL不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为画布生成一个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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆