0

我在 dojox gfx 中的事件处理有问题。

考虑以下代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Example</title>
    <!-- load Dojo -->
    <script>
        dojoConfig = {
        async: true,
        gfxRenderer: "svg,silverlight,vml" // svg gets priority
    };
    </script>
    <script src="dojo/dojo.js"></script>
</head>
<body>
<script>
require(["dojox/gfx","dojox/gfx/Moveable","dojo/domReady!","dojox/gfx/fx"],function(gfx,Moveable,dom,gfxFx) {
    var surface = gfx.createSurface("surfaceElement", 400, 400);
    var rect = surface.createRect({ x: 30, y: 30, width: 100, height: 100 }).setFill('blue');
    rect.connect('onclick',function(e) {
        alert('first')
    });
    var otherRect = surface.createRect({ x: 30, y: 30, width: 100, height: 50 }).setFill('red');
}); 
</script>
<!-- DOM element which will become the surface -->
<div id="surfaceElement"></div>
</body>
</html>

当我点击红色矩形时,有没有办法处理蓝色矩形的事件?谢谢

4

1 回答 1

0

只需将第二个矩形创建移动到事件侦听器前面,然后通过变量引用它:

require(["dojox/gfx","dojox/gfx/Moveable","dojo/domReady!","dojox/gfx/fx"],function(gfx,Moveable,dom,gfxFx) {
    var surface = gfx.createSurface("surfaceElement", 400, 400);
    var rect = surface.createRect({ x: 30, y: 30, width: 100, height: 100 }).setFill('blue');
    var otherRect = surface.createRect({ x: 30, y: 30, width: 100, height: 50 }).setFill('red');
    rect.connect('onclick',function(e) {
        // otherRect is available here
        console.log(otherRect);
    });
});
于 2012-11-09T13:28:14.677 回答