jQuery focus/blur on form, not individual inputs

后端 未结 4 649
臣服心动
臣服心动 2020-12-18 05:46

How could this be achieved via jQuery?

When the user clicks on any input in the form, it is focussed. When a user clicks out of the form from any input it is blurred

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 06:33

    Here's the same basic idea as Alexey's but for jQuery 1.7+ and, at least to my mind, a little more readable because it keeps everything in scope within the same handler:

        $("#myform").on("focusout focusin", "input", function(e){
            if ( e.type == "focusout" ) {
                $("#myform").attr("form-blur-timeout", window.setTimeout(function(){
                    console.log("you've left the form!");//do stuff here
                }, 100));
            } else {
                window.clearTimeout($("#myform").attr("form-blur-timeout"));
            }
        });
    

    Enjoy :)

提交回复
热议问题