我的表单中有3个输入字段(文本字段)。
<input id="input1" name="input1" type="text" value="">
<input id="input2" name="input2" type="text" value="">
<input id="input3" name="input3" type="text" value="">
我也有这段JavaScript:
$("#timetable").on("submit", function(event) {
console.log("input " + $("input").attr("id") + "has focus");
});
我想知道按下输入/返回按钮时哪个输入字段具有焦点。我已经搜寻过网,但是却空无一人。
谢谢
Document.activeElement
返回当前关注的元素,即,如果用户键入任何元素将获得击键事件的元素。该属性是只读的。
因此,在Submit事件侦听器中,您将访问document.activeElement
,这是关注的元素。
$("#timetable").on("submit", function (event) {
console.log(document.activeElement); // element
console.log(document.activeElement.id); // element id
$(document.activeElement).css('border-color', 'red');
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句