3

其实我的问题是不同的。

我在我的小盒子中的第一个表单元素的 .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()”,因为这对所有小盒子都很常见。

4

3 回答 3

2

“焦点”函数/绑定/事件处理程序到元素。

尝试新的“on”函数,它/adds/ 一个事件处理程序。

$(".tbox :input:visible:enabled:first").on('focus', function () {
        tabIndexForm();
});

编辑第一次尝试不起作用。

这应该会更好:

$(".tbox").on('focus', ":input:visible:enabled:first", function () {
        tabIndexForm();
});

这样,事件处理程序就不会附加到元素上,因此它不会覆盖旧的。

于 2012-11-02T09:42:24.010 回答
1

这应该有效:

var elem = $(".tbox :input:visible:enabled:first");
var oldFunc = elem[0].onfocus;
elem.focus(function () {
    if(typeof oldFunc == 'function') oldFunc.call(elem[0]);
    tabIndexForm();
});

检查这个演示:http: //jsfiddle.net/8JbvY/

于 2012-11-02T09:40:27.890 回答
1

onfocus从元素中删除属性:

<form name="exampleform">
  <input type="text" name="a"/>
  <input type="text" name="b"/>
</form>

并将您的 javascript 更改为:

$(".tbox input[name='a']").focus(somefunction);

$(".tbox :input:visible:enabled:first").focus(function () {
     tabIndexForm();
});

这也是处理事件处理程序的一种更简洁的方式,因为您将逻辑保存在一个地方。

于 2012-11-02T09:43:28.103 回答