其实我的问题有些不同。
我在我的小盒子中的第一个表单元素的 .focus 事件上调用了一个函数,如下所示。
$(".tbox :input:visible:enabled:first").focus(function () {
tabIndexForm();
});
它工作得很好,但这取代了我自己在我手动设置的表单的第一个元素上调用 onfocus 事件的函数。这个怎么做。我的表格是:
<form name="exampleform">
<input type="text" name="a" onfocus="somefunction();"/>
<input type="text" name="b"/>
</form>
现在,当我打开小盒子时,tabIndexForm 函数可以完美调用,但 somefunction() 无法正常工作,因为我认为 tabIndexForm 取代了这个函数。如何更新以下代码以使其同时工作。
$(".tbox :input:visible:enabled:first").focus(function () {
tabIndexForm();
});
重要提示:我不能在 tinybox.js 中调用“somefunction()”,因为这对所有小盒子都很常见。
最佳答案
“焦点”函数/绑定(bind)/一个事件处理程序到元素。
尝试新的“on”函数,它/添加/一个事件处理程序。
$(".tbox :input:visible:enabled:first").on('focus', function () {
tabIndexForm();
});
编辑第一次尝试不起作用。
这应该会更好:
$(".tbox").on('focus', ":input:visible:enabled:first", function () {
tabIndexForm();
});
这样事件处理程序就不会附加到元素上,所以它不会覆盖旧的。
关于javascript - 如何在jquery中的onfocus事件上调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13192638/