Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Image.onload事件在加载映像之前触发要触发的apears_Javascript_Jquery_Onload - Fatal编程技术网

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
。我已经直接尝试过了n
targetImage
和on
targetImage.src
似乎没有什么区别。在第二点上,我很清楚它应该出现在图像上,而不是图像的字符串属性。你最好注册事件,而不是使用
onload
属性。在设置之前,你应该注册它点击
src
,否则图像可能已经加载,如果图像已经加载,
onload
将不会启动。另外的页面没有建议将函数设置为数字,因为这也没有任何意义。