如何在javascript中动态添加图像 [英] How to add an image dynamically in javascript

查看:78
本文介绍了如何在javascript中动态添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据行的列中的flag变量的条件添加图像。我无法使用innerHtml或如果我也使用图像对象,它无法正常工作。行是动态创建的,所以我很困惑如何解决它,当flag为true时,它们将显示图像,否则图像将不会显示。

I want to add an image according to the condition of flag variable in a column of the row. I am unable to use the innerHtml or if I use the image object also, it's not working properly. The rows are created dynamically so I am confused how I can solve it, when flag is true them image will be shown otherwise image will not be shown.

推荐答案

只需将您的图像(可能是标记)保存在静态位置

Just keep your Image( may be tag) where you want statically as

<IMG src="youImagePath" style="display:none;" id="myId"/>

然后在Javascript中你可以将此图像显示为

and then in Javascript you can show this Image as

 if(flag){
    document.getElementById('myId').style.display="block";
}

如果您有一些复杂的要求,那么@Christopher Tokar解决方案会更好。

And if you have some complicated requrements then @Christopher Tokar solution is better.

谢谢

这篇关于如何在javascript中动态添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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