gpt4 book ai didi

javascript - 如何将图像(在 Canvas 后面)推到前面以使其 onclick 功能起作用

转载 作者:太空宇宙 更新时间:2023-11-03 21:36:36 24 4
gpt4 key购买 nike

我有棋盘上棋子的图像。
该图像具有 onclick 功能,当您按下它时,您应该收到警报。
问题是图像上方的 Canvas 隐藏了 onclick 的功能。

当我放置鼠标光标(使用 Chrome 的放大镜)时,您可以看到 Canvas 位于 pawn 图像的顶部:
enter image description here

带 Canvas :
JSFIDDLE: when click pawn nothing happens

没有 Canvas :
JSFIDDLE: when click pawn receive alert

如何保留 Canvas 并将 pawn 图像推到前面,以便 onclick 函数起作用?

CSS:

td {
width: 100px;
height: 90px;
text-align: left;
vertical-align: top;
border: 1px solid black;
position: relative;
}
table
{
position: fixed;
left:9px;
top:8px;
}

tr:nth-child(even) td:nth-child(odd), tr:nth-child(odd) td:nth-child(even)
{
background:#00A2E8;
}

td span {
position: absolute;
bottom: 0;
}

#myCanvas {
z-index: 10;
position:absolute;
font:bold;
color:red;

}

HTML:

<body>

<div id="board" value="5">

<table oncontextmenu="return false">
<tbody>
<tr>
<td class="" cellnumber="21" row="4" col="0"><span>21</span></td>
<td class="" cellnumber="22" row="4" col="1"><span>22</span>
<br><br><p class="SnakesAndLadders" from="22" to="6">Snake to 6 </p></td>
<td class="" cellnumber="23" row="4" col="2"><span>23</span></td>
<td class="" cellnumber="24" row="4" col="3"><span>24</span></td>
<td class="" cellnumber="25" row="4" col="4"><span>25</span></td>
</tr>
<tr>
<td class="" cellnumber="16" row="3" col="0"><span>16</span></td>
<td class="" cellnumber="17" row="3" col="1"><span>17</span></td>
<td class="" cellnumber="18" row="3" col="2"><span>18</span></td>
<td class="" cellnumber="19" row="3" col="3"><span>19</span></td>
<td class="" cellnumber="20" row="3" col="4"><span>20</span></td></tr>
<tr>
<td class="" cellnumber="11" row="2" col="0"><span>11</span></td>
<td class="" cellnumber="12" row="2" col="1"><span>12</span></td>
<td class="" cellnumber="13" row="2" col="2"><span>13</span></td>
<td class="" cellnumber="14" row="2" col="3"><span>14</span></td>
<td class="" cellnumber="15" row="2" col="4"><span>15</span>
<br><br><p class="SnakesAndLadders" from="15" to="24">Ladder to 24 </p>
</td>
</tr>
<tr>
<td class="" cellnumber="6" row="1" col="0"><span>6</span></td>
<td class="" cellnumber="7" row="1" col="1"><span>7</span></td>
<td class="" cellnumber="8" row="1" col="2"><span>8</span></td>
<td class="" cellnumber="9" row="1" col="3"><span>9</span></td>
<td class="" cellnumber="10" row="1" col="4"><span>10</span></td>
</tr>
<tr>
<td class="" cellnumber="1" row="0" col="0"><span>1</span>
<img src = "http://s23.postimg.org/ynlvim1x3/image.png" onclick="doSomething()">
</td>
<td class="" cellnumber="2" row="0" col="1"><span>2</span></td>
<td class="" cellnumber="3" row="0" col="2"><span>3</span></td>
<td class="" cellnumber="4" row="0" col="3"><span>4</span></td>
<td class="" cellnumber="5" row="0" col="4"><span>5</span></td>
</tr>

</table>
<canvas id="myCanvas" width="600" height="500"></canvas>
<canvas id="myCanvas2" width="600" height="500"></canvas>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

context.beginPath();
context.moveTo(50, 45);
context.lineTo(500, 450);
context.stroke();
context.stroke();

function doSomething()
{
alert("Ping");
}
</script>

最佳答案

如果 Canvas 除了画线什么都不做,那么就告诉浏览器不要监听 Canvas 上的事件:

#myCanvas{
pointer-events: none;
}

演示:http://jsfiddle.net/m1erickson/La292q67/5/

关于javascript - 如何将图像(在 Canvas 后面)推到前面以使其 onclick 功能起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26107207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com