开发者

Jquery 'or' statement?

开发者 https://www.devze.com 2023-03-28 23:41 出处:网络
I would like for a text box to disappear if a p开发者_如何学JAVAerson clicks out side of it (.focusout or .blur) but I also want to be able do the same function if Esc is pressed (key 27). I am not su

I would like for a text box to disappear if a p开发者_如何学JAVAerson clicks out side of it (.focusout or .blur) but I also want to be able do the same function if Esc is pressed (key 27). I am not sure how to get both to be recognized without ruining my entire script.


You can bind both events to the same function with

$(selector).bind("blur keyup",function(e){...})

And then it's just decision logic to see if it was a key that was pressed or whatnot.


How about something like this:

$('#selector').bind('blur keyup', function(e) {
    if(e.keyCode == 27 || e.keyCode == undefined)
        $(this).hide();
});


I would create a custom event bound to the text box:

<script>
$(document).ready(function(){
   $("#id").bind("blur keyup myCustomEvent"),function(event){
    //whatever you need
    // you can trigger this logic again via .trigger("myCustomEvent");
    });

});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号