我正试图在按下按钮时获得最后一个焦点元素,但我运气不佳。这是我的javascript函数:
function input(e) {
e = e.charAt(3);
$(".textboxclass").each(function(index, value) {
$(this).val();
if ($(this).is(':focus')) {
$(this).val($(this).val() + e);
$(this).focus();
}
});
}
且以下行不工作
$(this).is(':focus')
当我在alert()工作之前设置它时。
谢谢。
它之所以不起作用,是因为每次单击按钮,最后一个聚焦的文本框都会失去焦点,你需要找到另一种方法来做你想做的事情。我可以想到的一种方法是使用一种方法,将元素集中存储在全局变量中,然后在yout if
条件中使用它。试试看:
var $focused;
$(".textboxclass").focus(function(){
$focused = $(this);
});
function test(id) {
e = id.charAt(3);
$(".textboxclass").each(function() {
if ($(this)[0] == $focused[0]) {
$(this).val($(this).val() + e);
$(this).focus();
} else {
console.log("NOT focused");
}
});
}