代码将图像预加载到图像对象中,然后(假设)将其设置为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="myImage" src="" />
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为什么它不起作用?
尝试
<!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>
Run Code Online (Sandbox Code Playgroud)
您无法将src设置为图像,您必须将其设置为图像的src.(PS:最后添加分号并更改.src = oImage为.src = oImage.src)
| 归档时间: |
|
| 查看次数: |
8410 次 |
| 最近记录: |