如何从div调用javaScript函数

时间:2013-05-10 10:24:00

标签: html javascript

你好,有人知道如何从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>

1 个答案:

答案 0 :(得分:1)

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

看到这个答案 How to add onload event to a div element?