2013-05-10 72 views
-1

hello是否有人知道如何從div調用JavaScript函數? 我的股利和功能如下所示:如何從div調用javaScript函數

@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> 
+0

你必須使用原始的JavaScript或者你有jQuery的在您的處置? – 2013-05-10 10:29:07

+0

現在即時通訊使用JavaScript,但如果你有一個更好的主意,我可以嘗試.. – 2013-05-11 09:58:34

+1

可能重複的[如何將onload事件添加到一個div?](http://stackoverflow.com/questions/4057236/how-to- add-onload-event-to-a-div) – DanMan 2014-03-04 12:36:11

回答