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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:46:00  来源:igfitidea点击:

Creating image object with local image using javascript

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 imgelement like any other element: createElement. (There's also new Image()which is pretty much the same thing, just a legacy mechanism.)

  • You can set its source by setting the srcproperty, which can be a file://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 imgelement in the document by appending it to whatever other element you want to append it to, via appendChildor related methods.

  • You can findan existing element using getElementById, getElementsByTagName, etc., or (on most browsers but not all) 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。或者清楚地解释你的问题。