javascript 使用javascript动态加载图像和自定义加载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27558193/
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
Dynamic load image & Custom Load image using javascript
提问by Mahadevan Sivasubramanian
Currently working on Loading image dynamic. If the custom image has the path it has to take the custom image not the default one if there is no custom image path are not available it has to load default image in the given ID.
目前正在加载动态图像。如果自定义图像具有路径,则必须采用自定义图像而不是默认图像,如果没有自定义图像路径不可用,则必须加载给定 ID 中的默认图像。
As I am new to Javascript I am trying my below code.
因为我是 Javascript 的新手,所以我正在尝试下面的代码。
var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
function loadGraphics(){
document.getElementById("loadImage").src = defaultImageurl;
}
loadGraphics();
<div id="loadImage"></div>
It was not working kindly help me.
它不起作用,请帮助我。
Thanks in advance.
提前致谢。
Regards Mahadevan
问候马哈德万
回答by Vince
Your problem is that a div has no src
attribute, as in your example you have <div id="loadImage"></div>
您的问题是 div 没有src
属性,就像在您的示例中一样<div id="loadImage"></div>
Your defaultImageurl
is also empty.
你defaultImageurl
的也是空的。
The solution is to use an <img/>
element:
解决方案是使用一个<img/>
元素:
var customImageurl = "custom_Image/web.png";
var defaultImageurl = "";
function loadGraphics(){
//v-- will not work!
document.getElementById("loadImage").src = defaultImageurl;
//v-- will work given your example conditions
document.getElementById("loadImage").src = customImageurl;
}
window.onload = function(){
loadGraphics();
};
<div id="loadImage-wrap">
<img id="loadImage" />
</div>
回答by user3292788
You are trying to use a src
attribute on a <div>
element.
您正在尝试src
在<div>
元素上使用属性。