-1

你好,有人知道如何从 div 调用 JavaScript 函数吗?我的 div 和函数如下所示:

@foreach(var c in Model.matrix.Cells)
{
    <tr>
     <td class="tdHouse" >
        <div onload="styleCell(event)" class="cell" id="styleCell" ondrop="drop(event,@c.row,@c.column)" ondragover="allowDrop(event)" alt="one">                                                                
        </div>   
            <label hidden id="@c.room.Id" class="existRoomStyle" ondragstart="drag(event)" onmouseup="dragMouseUp(event)" title="@c.room.roomName"  style= "background-color:@c.room.color"> @c.room.roomName</label>                                  
            <img  hidden id="@c.socket.Id" alt="existSocket" class="existSocketStyle" src="~/Images/socket.jpg"  draggable="true" ondragstart="drag(event)" onmouseup="dragMouseUp(event)" title="socket" alt="one"/>                                       
            <img  hidden id="@c.socket.machine.Id"class="existMachineStyle" src="~/Images/MachineImages/@c.socket.machine.Image" draggable="true" ondragstart="drag(event)" title="machine" alt="one"/>                                      

     </td>
   </tr>                        
  }

<script>
function styleCell(ev) {
    ev.target.children[0].appendChild(document.getElementById("existRoomStyle"));
    ev.target.children[0].appendChild(document.getElementById("existSocketStyle"));
    ev.target.children[0].appendChild(document.getElementById("existMachineStyle"));
 }
</script>
4

1 回答 1

1

你不能以这种方式调用 onload 。 w3schools list where no-load is applicable 您必须将函数调用与 div 分开。

请参阅此答案 如何将 onload 事件添加到 div 元素?

于 2013-05-10T12:43:51.707 回答