2013-05-10 117 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

回答