3

正如你在我的小提琴中看到的那样。或者在我下面的代码中,我试图让一个事件发生。将鼠标悬停在框上并单击空格键时,将出现确认窗口。单击“是”后,该框将被删除。

错误/问题是,一旦您删除该框并再次单击空格键,它就会出现相同的提示。我以为我已经通过关闭监听解决了这个问题rTwo.setListening(false); layer.drawHit();

无论如何,任何帮助将不胜感激。谢谢。

代码:

var stage = new Kinetic.Stage({
    container: 'container',
    width: 850,
    height: 400
});
var layer = new Kinetic.Layer();
var rTwo = new Kinetic.Rect({
    x: 0,
    y: 0,
    width: 100,
    height: 50,
    fill: 'blue',
    draggable: true,
});
layer.add(rTwo);
rTwo.on('mouseover', function() {
    document.onkeypress = function(e) {
        e = e || window.event;
        var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
        if (charCode == 32) {
            var b1 = confirm("Would you like to delete router 2?");
            if (b1 == true) {
                rTwo.hide();
                rLayer.draw();
                rTwo.setListening(false);
                rlayer.drawHit();
            } else if (b1 == false) {
                rLayer.draw();
            }
        }
    };
});
stage.add(layer);
4

1 回答 1

1

http://jsfiddle.net/gSEeb/4/

所以这是你的逻辑:在鼠标悬停时你注册 onkeypress 函数,它检查按键,如果那个键是空格键,然后显示一个提示。

mouseover-> 注册函数

功能 -> 检查按键 -> 是空格键吗?-> 显示提示

如果你看这个,你会检查空格键是否是鼠标悬停,因为鼠标悬停只是注册一个函数。基本上你需要重新检查你的鼠标是否在矩形中。

 rTwo.on('mouseover mousemove', function(){
      document.onkeypress = function(e) { 
           e = e || window.event;
           var charCode = (typeof e.which == "number") ? e.which : e.keyCode;
           if (charCode == 32 && rTwo.intersects(stage.getUserPosition())) {
                var b1 = confirm("Would you like to delete router 2?");
                if (b1 == true){
                     rTwo.hide();
                     layer.drawHit();  // or whichever layer rTwo is in
                     layer.draw();
                     rTwo.setListening(false);
                }
                else if (b1 == false){
                     layer.draw();  // you don't really need to redraw this here as nothing is changed
                }
           }
      };
 });
于 2013-01-15T14:51:01.140 回答