简体   繁体   English

隐藏元素按Esc

[英]hide element press Esc

This code doesn't hide the box div which was supposed to be hidden when I press the Esc key. 此代码不会隐藏我按Esc键时应该隐藏的box div

function Boxup(elementN, event){
    $("#"+elementN).css({
        "display":"block",
        "top":event.pageY+"px" ,
        "left":event.pageX+"px"    
    })
}
function hideCurrentPopup(ele){
    $(ele).parent().hide();
}

    $(this).keyup(function(event) {
        if (event.which == 27) { 
            disablePopup();  
        }   
    });

Am I missing something? 我想念什么吗?

From your code I cannot exactly tell what's this referring to in this line: 从你的代码,我不能确切地告诉什么是this指的是在这一行:

$(this).keyup(function(event) {

cause it this refers to a "textarea" or "input" it will trigger the event if that element has focus , otherwise you're looking for keyup events registered by document 导致它this是指"textarea""input" ,将触发该事件,如果该元素具有焦点 ,否则你正在寻找keyup通过注册的事件document

but here's what you can try. 但您可以尝试以下方法。

function Boxup(elementN, event){
    $("#"+elementN).css({
        display : "block",
        top  : event.pageY , // px are not needed as they are default unit in jQ
        left : event.pageX    
    })
}

function hideCurrentPopup(ele){ // note your function name and the argument! 
    $(ele).parent().hide();     // (do you need .parent()? I don't know
}                               // without seeing any HTML sample)

$(document).keyup(function(event) { // document is probably the selector you want
    if (event.which == 27) { 
        hideCurrentPopup("#hereYourPopupID");  // try alike
    }   
});

PS: Make sure that by using $(some Selector here).keyup(function(event) { you're not preventing in any case a keyup event to bubble up the DOM tree to reach the documentElement PS:请确保通过使用$(some Selector here).keyup(function(event) {在任何情况下,您都不会阻止通过keyup事件使DOM树冒泡以到达documentElement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM