我正在尝试在单击事件中预加载图像:
// new image object
var imgObject = new Image();
// assign the path to the image to the src property
imgObject.src = document.URL + 'image/image.jpg';
// check if image has loaded
if (imgObject.complete) {
但是完整的调用在第一次点击时永远不会返回true - 你知道我在这里缺少什么吗?
.complete
是一个属性,用于说明图像是否已加载,而不是您可以附加到的事件。您的if
语句在设置图像src
后立即运行 - 在那个时候它可能永远不会成立(除非它被缓存了?)。我稍微更新了答案,以使其更清晰一些。 - James Hill