[英]How to access the canvas using jquery?
我無法使用jquery訪問畫布ID。
我添加了jQuery庫。 但是當我點擊畫布ID時,我無法收到警報。
但是javascript工作正常。
<script>
$(document).ready(function(){
$('#sq').click(function(){
alert('test');
});
});
</script>
<canvas id="example" width="260" height="200">
<h2>Shapes</h2>
<p>A rectangle with a black border. In the background is a pink circle. Partially overlaying the
<a href="http://en.wikipedia.org/wiki/Circle" onfocus="drawCircle();" onblur="drawPicture();">circle</a>.
Partially overlaying the circle is a green
<a href="http://en.wikipedia.org/wiki/Square" onfocus="drawSquare();" onblur="drawPicture();" id="sq">square</a>
and a purple <a href="http://en.wikipedia.org/wiki/Triangle" onfocus="drawTriangle();" onblur="drawPicture();">triangle</a>,
both of which are semi-opaque, so the full circle can be seen underneath.</p>
</canvas>
您不應該在canvas標記中放置html元素。 畫布的想法是使用代碼(也稱為javascript) 繪制它。 你正在做的事情不會起作用(或者只有當瀏覽器不支持畫布時才更有可能看到 - 這意味着非常古老的瀏覽器):)
從畫布中取出html元素並更改$('#sq').click(function(){
to $('#example').click(function(){
看看這個: http : //jsfiddle.net/Zprb4/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.