Html 如何从div调用javaScript函数
您好,有人知道如何从div调用JavaScript函数吗? 我的div和函数如下所示:Html 如何从div调用javaScript函数,html,javascript,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
@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>
@foreach(Model.matrix.Cells中的变量c)
{
@c、 room.roomName
}
功能电池(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”);
}
您不能以这种方式调用onload。
必须将函数调用与div分开
看到这个答案了吗
你必须使用原始javascript还是有jquery供你使用?现在我使用javascript,但如果你有更好的想法,我可以试试。你的问题可能重复的地方还不清楚。div是一个HTML元素,不能“调用”函数。如果在div出现之后运行某些东西非常重要,那么只需将JS调用放在HTML中div之后的某个地方。