简体   繁体   English

如何在html img标签中使用Java字符串图像对象-有可能吗?

[英]How can I use a java string image object in a html img tag - Is it possible?

I am currently generating a javascript image from a base64 string. 我目前正在从base64字符串生成JavaScript图片。 I am doing something like this 我正在做这样的事情

function generateHTML() 
{
  var image = new Image();
  var src = "data:image/jpeg;base64," + picString;
  image.src = src;
  ......
}

Now I know i can do something like this 现在我知道我可以做这样的事情

document.body.appendChild(image);

but that is not what I want. 但这不是我想要的。 In my javascript function I am returning back an html string. 在我的javascript函数中,我返回了一个html字符串。 My question is if it is possible for me to use the javascript image with a html statement like this 我的问题是,是否可以将javascript图像与类似html的语句一起使用

<img src=image alt="Mountain View" style="width:304px;height:228px;">

You mean like this, where it uses the img id to append the src? 您的意思是这样,它使用img id附加src?

 function generateHTML() { var picString = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="; var image = new Image(); var src = "data:image/jpeg;base64," + picString; document.getElementById('img').src = src; } generateHTML(); 
 <img id="img" alt="Mountain View" title="Red Dot" /> 

If you are looking to get the html string of an element and not looking to append it to the current document, use outerHTML on the Image instance you created. 如果要获取元素的html字符串,而不是希望将其附加到当前文档,请在创建的Image实例上使用externalHTML

function generateHTML() {
  var image = new Image();
  var src = "data:image/jpeg;base64," + picString;
  image.src = src;
  return image.outerHTML;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML `img` 标签无法在对象 jquery 中显示为图像 - HTML `img` tag can't display as image in object jquery 如何在图像的源标签中使用返回的字符串(React v16.0.0 的新特性):<img src='returnedString' /> ? - How Can I use the returned string (new feature of React v16.0.0) in the source tag of an image ex: <img src='returnedString' />? 如何使用html文件作为图像文件的来源 <img src=“”> 标签 - How to use a html file as a source of image file in <img src=“”> tag 如何在我的 html 中使用 json 脚本<script> tag and populate the data in the <img src=???/> - How can I use the json script on my html in the <script> tag and populate the data in the <img src=???/> 如何使用“.replace()”和“.html()”将纯文本转换为图像标记? - How can I use “.replace()” and “.html()” to turn plaintext into an image tag? 如何在第一个img标签之前和2 img标签之后添加html代码 - how can I add html code before the 1st img tag and after 2 img tag 如何下载使用渲染的图像<img>标签? - How can I download image which is rendered with <img> tag? 如何将属性添加到img标签以在ajax调用中使用 - How can I add attributes to an img tag to use in an ajax call 我该如何使用<img>在 Android 上使用本地图像? - How can I use <img> with a local image on Android? 如何模糊图像的一部分 <img> 使用CSS标记,我没有使用背景图片的选项 - How to blur part of an image <img> tag using css, I don't have an option to use background image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM