[英]keep focus on text input field if another input field is not clicked
我正在尝试创建一个onblur事件,只要有人在页面上的任意位置单击它,它将一直专注于该元素,除非它是一个specif元素,然后它将重新聚焦于该特定元素。 我可能还很遥远,但这是我尝试过的方法,无论如何,它始终专注于Barcode元素。
function stayOnBarcode() {
var QTY = document.getElementById("QTY");
var Barcode = document.getElementById("Barcode");
if (Barcode.value === "") {
if (QTY.focus() === true) {
QTY.focus();
}
else if (document.hasFocus() === true) {
Barcode.focus();
}
}
else {
Barcode.focus();
}
}
这样的事情怎么样:
$(document).on("mousedown", function(e) {
clicked = $(e.target)
})
$("input").on("blur", function() {
if (!clicked.is("#QTY") && !clicked.is("#Barcode")) {
$(this).focus()
}
})
它存储在变量最近点击的元素clicked
,然后在blur
它检查事件last_clicked
比其他东西#QTY
或#Barcode
。 如果是这样,它将重新聚焦模糊的input
。
您可能需要对其进行调整以适合您所考虑的确切条件,或者随时为您的问题添加细节。
查看此工作提琴: https : //jsfiddle.net/oez0488h/63/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.