onload函数不在图像和输入标签中执行

时间:2019-05-28 08:59:55

标签: javascript php html

我试图显示存储在数据库中的图像。但是,我在尝试使用它来显示图像的onload函数上遇到问题。

我已经尝试过window.onload函数,但是它根本不起作用,因为我需要将参数传递给函数(不确定是否可以直接传递)。 我尝试过的事情

document.getElementById("albumImage").addEventListener("load", setAlbum($patient,$image));

<body onload="startLoad({{$patient}},{{$image}})"

<script>
function startLoad($patient, $image){
      function setProfilePic($patient);
      function setAlbum($patient, $image);
}
</script>

但是他们两个都不执行该功能。

第一个代码是我试图显示个人资料图片。

<img id="profilePicID" src="" onload="setProfilePic({{$patient}})">

第二个代码是我试图显示相册中的图片(我将图片存储为带','的字符串)

@if(strpos($patient->album, ',') !== false)
        <td id="albumBody">
        @foreach(explode(',', $patient->album) as $image)
        <input id="albumImage" type="image" onload="setAlbum({{$patient}}, {{$image}})">
        @endforeach
@endif

下面是我的脚本

function setProfilePic($patient){
      document.getElementById("test").innerHTML = "Test success 1";
      document.getElementById("profilePicID").src = '../../storage/images/'+$patient.name+'/'+$patient.profilePic;
    }

    function setAlbum($patient, $image){
      document.getElementById("test").innerHTML = "Test success 2";
      var body = document.getElementById("albumBody");//td
      var x = document.createElement("IMG");
      x.setAttribute("src", '../../storage/images/'+$patient.name+'/'+$image);
      x.setAttribute("width", "200");
      x.setAttribute("height", "200");
      x.setAttribute("style", "transform:rotate(90deg);");
      body.appendChild(x);
    }

希望你们能帮助我。

0 个答案:

没有答案