本文介绍了Javascript:image object - > HTML图像元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码将图像预加载到图像对象中,然后(应该)将其设置为HTML上的图像元素src:
<!DOCTYPE html>
< html>
< head>
< script language =javascript>
window.onload = function(){
var oImage = new Image();
oImage.onload = function(){
document.getElementById('myImage')。src = oImage;
alert('done');
};
oImage.src ='image1.jpg';
}
< / script>
< / head>
< body>
< img id =myImagesrc =/>
< / body>
< / html>
为什么它不起作用?
解决方案
试试
<!DOCTYPE html>
< html>
< head>
< script language =javascript>
window.onload = function(){
var oImage = new Image();
oImage.onload = function(){
document.getElementById('myImage')。src = oImage.src;
alert('done');
};
oImage.src ='image1.jpg';
};
< / script>
< / head>
< body>
< img id =myImagesrc =/>
< / body>
< / html>
您无法为图片设置 src ,您必须将其设置为图像的 src。(PS:在末尾添加分号并将 .src = oImage
更改为 .src = oImage.src
)
The code preloads an image into an image object and then (supposed to) set it to the image element src on HTML:
<!DOCTYPE html>
<html>
<head>
<script language="javascript">
window.onload = function () {
var oImage = new Image();
oImage.onload = function () {
document.getElementById('myImage').src = oImage;
alert('done');
};
oImage.src = 'image1.jpg';
}
</script>
</head>
<body>
<img id="myImage" src="" />
</body>
</html>
Why it doesn't work?
解决方案
Try
<!DOCTYPE html>
<html>
<head>
<script language="javascript">
window.onload = function () {
var oImage = new Image();
oImage.onload = function () {
document.getElementById('myImage').src = oImage.src;
alert('done');
};
oImage.src = 'image1.jpg';
};
</script>
</head>
<body>
<img id="myImage" src="" />
</body>
</html>
You can't set a src to an image, you have to set it to the image's src. (PS: Added semicolon at the end and changed .src = oImage
to .src = oImage.src
)
这篇关于Javascript:image object - > HTML图像元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!