Javascript Image.onload事件在加载映像之前触发要触发的apears
我有一个函数,每次调用它时都会显示不同的图像Javascript Image.onload事件在加载映像之前触发要触发的apears,javascript,jquery,onload,Javascript,Jquery,Onload,我有一个函数,每次调用它时都会显示不同的图像 function Trial() { curTrial = increase(curTrial); //generate the html code $('#right_wrapper').append(` <div id="image_holder"> <img id="image"> </div>`) // present the image
function Trial() {
curTrial = increase(curTrial);
//generate the html code
$('#right_wrapper').append(`
<div id="image_holder">
<img id="image">
</div>`)
// present the image
var targetImage = document.getElementById('image')
targetImage.src = stimuli[curTrial];
console.log(targetImage.onload = targetImage.height)
功能试验(){
缩短=增加(缩短);
//生成html代码
$('#右包装器')。追加(`
我认为onload
的工作方式是,它在src
加载之前不会触发,但它似乎无论如何都会触发,从而导致卸载图像的高度为0。我发现了更多具有类似问题的线程,但我所见过的线程都不能处理模板文本
我如何确定targetImage.height
总是返回加载图像的高度,而不是0?它应该是targetImage.onload
,而不是targetImage.src.onload
,不是吗?而且onload
应该是一个函数,所以我不确定你是否可以将其设置为height
。我已经直接尝试过了ntargetImage
和ontargetImage.src
似乎没有什么区别。在第二点上,我很清楚它应该出现在图像上,而不是图像的字符串属性。你最好注册事件,而不是使用onload
属性。在设置之前,你应该注册它点击src
,否则图像可能已经加载,如果图像已经加载,onload
将不会启动。另外的页面没有建议将函数设置为数字,因为这也没有任何意义。