我需要获得在单击另一个元素时聚焦的文本区域id。
我使用$(':input:focus').attr('id')
,但点击后文本区域立即失去焦点,我无法获得文本区域的id。
有人能帮忙吗?
是的,您可以将id保存在全局变量中以获取它,并检查当前关注的输入类型。
喜欢它:
var areaId = $('textarea:focus').attr('id');
要么使用上面的代码,要么使用下面的代码:
var areaId = "";
//define this variable at the top of starting the javascript code.
areaId = $(':input:focus').attr('id');
也可以使用jQUery的focusout()
函数:
$(':input').focusout(function(){
var id = $(this).attr('id');
});
您可以使用.focusout()
方法:
$('#focusedItem').focusout(function() {
var id = $(this).attr('id');
});