javascript 使用javascript创建带有本地图像的图像对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9465657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating image object with local image using javascript
提问by user1232780
I want to create image object when user selecting a file from html page.
我想在用户从 html 页面选择文件时创建图像对象。
How can i create image object for dynamic images. Can anybody share me the sample snippet .
如何为动态图像创建图像对象。任何人都可以分享我的示例片段。
Thanks in advance
提前致谢
回答by T.J. Crowder
Basically:
基本上:
You can create an
img
element like any other element:createElement
. (There's alsonew Image()
which is pretty much the same thing, just a legacy mechanism.)You can set its source by setting the
src
property, which can be afile://
URL the user supplies......or you can let them choose via an
input type="file"
element iftheir browser supports using the File API; see my answer to this other questionfor getting access to image data that way.You can put the
img
element in the document by appending it to whatever other element you want to append it to, viaappendChild
or related methods.You can findan existing element using
getElementById
,getElementsByTagName
, etc., or (on most browsers but not all)querySelector
/querySelectorAll
.
您可以
img
像创建任何其他元素一样创建一个元素:createElement
。(也new Image()
有几乎相同的东西,只是一个遗留机制。)您可以通过设置
src
属性来设置其来源,该属性可以是file://
用户提供的URL......或者如果他们的浏览器支持使用File API,您可以让他们通过一个
input type="file"
元素进行选择;请参阅我对另一个问题的回答,以通过这种方式访问图像数据。您可以
img
通过将元素附加到要附加到的任何其他元素、通过appendChild
或相关方法来将元素放入文档中。您可以使用、等或(在大多数浏览器上但不是全部)/找到现有元素。
getElementById
getElementsByTagName
querySelector
querySelectorAll
In addition the various specifications linked inline above, you may also find the new work-in-progress HTML5 specificationuseful.
除了上面内嵌链接的各种规范之外,您可能还会发现新的正在进行中的HTML5 规范很有用。
回答by Praveen Vijayan
The following code will create an empty image object and it will set its src.
下面的代码将创建一个空的图像对象并设置它的 src。
var img = new Image();
img.src = "";
We are unable to identify your question & the purpose. please make a jsfiddle with minimum test case. Or explain your question clearly.
我们无法确定您的问题和目的。请用最少的测试用例制作一个jsfiddle。或者清楚地解释你的问题。