focusin()
将一个事件函数绑定到"focusin"事件。
.focusin(handler)
- handler类型: Function(Event eventObject)每次事件触发时会执行的函数。
.focusin([eventData ], handler)
- eventData类型: Anything。一个对象,它包含的数据键值对映射将被传递给事件处理程序。
- handler类型: Function(Event eventObject)每次事件触发时会执行的函数。
.focusin()
- 这个方法不接受任何参数。
这个方法的前两个用法是.bind('focusin', handler)
的快捷方式。第三个不带参数的用法是.trigger("focusin")
的快捷方式。
focusin
事件会在元素(或者其内部的任何元素)获得焦点时触发。这跟 focus 事件的显著区别在于,它可以在父元素上检测子元素获得焦点的情况(换而言之,它支持事件冒泡)。
此事件通常会和focusout事件一起使用。
Additional Notes(其他注意事项):
.focusin()
方法只是作为.on("focusin", handler)
的一个速记写法,移除该事件可以使用.off("focusin")
。
例子
监控页面上段落内获得焦点的情况。
<!DOCTYPE html> <html> <head> <style>span {display:none;}</style> <script src="https://www.lanmper.cn/static/js/jquery-3.5.0.js"></script> </head> <body> <p><input type="text" /> <span>focusin fire</span></p> <p><input type="password" /> <span>focusin fire</span></p> <script> $("p").focusin(function () { $(this ).find("span").css('display','inline').fadeOut(1000); }); </script> </body> </html>
focusin fire
focusin fire