Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在JQuery回调中获取元素_Javascript_Jquery - Fatal编程技术网

Javascript 在JQuery回调中获取元素

Javascript 在JQuery回调中获取元素,javascript,jquery,Javascript,Jquery,在jQuery回调函数中获取元素时遇到问题。我尝试过$this和$this.element的各种建议,但都没有成功 $.fn.onTypeFinished = function (func) { $(this).bind("keypress", onKeyPress) function onKeyPress() { setTimeout(onTimeOut, 500); } function onTimeO

在jQuery回调函数中获取元素时遇到问题。我尝试过$this和$this.element的各种建议,但都没有成功

$.fn.onTypeFinished = function (func) {
        $(this).bind("keypress", onKeyPress)
        function onKeyPress() {
            setTimeout(onTimeOut, 500);
        }

        function onTimeOut() {
            func.apply();
        }
        return this;
    };

    $(".user-input").onTypeFinished(function () {
        var ntabindex = parseFloat($(this.element).attr('tabindex'));
        ntabindex++;
        $('input[tabindex=' + ntabindex + ']').focus();
    });
我一直在想,我需要某种方法来将对这个函数的引用传递给回调函数,但似乎没有其他类似的帖子建议使用这种方法

下面是我正在尝试做的一些事情。该功能的要点是,我试图根据tabindex自动前进到下一个输入


感谢

根据日志,jQuery fn中的this关键字似乎指向窗口对象,我已按如下方式绑定函数并将其传递到apply中,并使其正常工作,从而使其指向输入元素:

    $.fn.onTypeFinished = function (func) {
        var time;
        $(this).each(function () {
            $(this).on('keypress', onKeyPress);
        });

        function onKeyPress() {
            clearTimeout(time);
            time = setTimeout(onTimeOut.bind(this), 500);
        }

        function onTimeOut() {
            func.apply(this);
        }
        return this;
    };
    $(".user-input").onTypeFinished(function () {
        //$('input[tabindex=3]').focus();
        var ntabindex = parseFloat($(this).attr('tabindex'));
        ++ntabindex;
        $('input[tabindex=' + ntabindex + ']').focus();
    });

当选择器返回一个输入数组时,还必须遍历元素数组,因此必须单独处理它们,否则事件只会发生一次,我已经对它进行了测试,它从第一个输入到第二个输入,然后停止工作。使用我粘贴在这里的方法,它将从一个输入转到另一个输入。

根据日志,jQuery fn中的this关键字似乎指向窗口对象,我已按如下方式绑定函数,并将其传递到apply中,并使其工作,从而改为指向输入元素:

    $.fn.onTypeFinished = function (func) {
        var time;
        $(this).each(function () {
            $(this).on('keypress', onKeyPress);
        });

        function onKeyPress() {
            clearTimeout(time);
            time = setTimeout(onTimeOut.bind(this), 500);
        }

        function onTimeOut() {
            func.apply(this);
        }
        return this;
    };
    $(".user-input").onTypeFinished(function () {
        //$('input[tabindex=3]').focus();
        var ntabindex = parseFloat($(this).attr('tabindex'));
        ++ntabindex;
        $('input[tabindex=' + ntabindex + ']').focus();
    });

当选择器返回一个输入数组时,还必须遍历元素数组,因此必须单独处理它们,否则事件只会发生一次,我已经对它进行了测试,它从第一个输入到第二个输入,然后停止工作。使用我粘贴在这里的方法,它将从一个输入转到另一个输入。

您应该了解,每当您更改范围时,这种情况都会发生变化。始终var$this=$this将其锁定

 $.fn.onTypeFinished = function (func) {
      var $this = $(this);
      $this.bind("keypress", onKeyPress);
      function onKeyPress() {
            setTimeout(onTimeOut, 500);
      }

      function onTimeOut() {
            func($this); //I would personally send this, like how you do $.each(function(count, element){})
      }
      return this;
  };

  $(".user-input").onTypeFinished(function (t) {
      var ntabindex = parseInt($(t).attr('tabindex')) + 1; //You should parse int here
      $('input[tabindex=' + ntabindex + ']').focus();
});
当然,您可以将以上内容简化为:
jsfiddle:

您应该理解,只要您更改范围,这种情况就会发生变化。始终var$this=$this将其锁定

 $.fn.onTypeFinished = function (func) {
      var $this = $(this);
      $this.bind("keypress", onKeyPress);
      function onKeyPress() {
            setTimeout(onTimeOut, 500);
      }

      function onTimeOut() {
            func($this); //I would personally send this, like how you do $.each(function(count, element){})
      }
      return this;
  };

  $(".user-input").onTypeFinished(function (t) {
      var ntabindex = parseInt($(t).attr('tabindex')) + 1; //You should parse int here
      $('input[tabindex=' + ntabindex + ']').focus();
});
当然,您可以将以上内容简化为:
jsfiddle:

从代码中可以看出,您希望将发生按键的元素传递给回调

为此,您需要将正确的上下文传递给超时处理程序,我们可以使用.bind,如下所示

此外,为了使计时器正常工作,还进行了一些其他修正

$.fn.onTypeFinished=functionfunc{ $this.bindkeypress,onKeyPress 按键功能{ var onTypeFinished=$this.data'onTypeFinished'; 如果!完成了{ onTypeFinished={}; $this.data'onTypeFinished',onTypeFinished; } clearTimeoutonTypeFinished.keytimer; onTypeFinished.keytimer=setTimeoutonTimeOut.bindthis,500; } 超时功能{ 应用函数; } 归还这个; }; $.user-input.onTypeFinishedfunction{ //$'input[tabindex=3]'。焦点; var ntabindex=parseFloatthis.getAttribute'tabindex'; ntabindex++; $'input[tabindex='+ntabindex+']'。焦点; };
从您的代码来看,您似乎希望将发生按键操作的元素传递给回调

为此,您需要将正确的上下文传递给超时处理程序,我们可以使用.bind,如下所示

此外,为了使计时器正常工作,还进行了一些其他修正

$.fn.onTypeFinished=functionfunc{ $this.bindkeypress,onKeyPress 按键功能{ var onTypeFinished=$this.data'onTypeFinished'; 如果!完成了{ onTypeFinished={}; $this.data'onTypeFinished',onTypeFinished; } clearTimeoutonTypeFinished.keytimer; onTypeFinished.keytimer=setTimeoutonTimeOut.bindthis,500; } 超时功能{ 应用函数; } 归还这个; }; $.user-input.onTypeFinishedfunction{ //$'input[tabindex=3]'。焦点; var ntabindex=parseFloatthis.getAttribute'tabindex'; ntabindex++; $'input[tabindex='+ntabindex+']'。焦点; };
这是朝着正确方向迈出的一步,但它还需要确定哪些输入实际上是范围内的重点元素。它实际上也只执行一次。然后它完全因为解除绑定而停止工作。这是朝着正确方向迈出的一步,但它还需要确定哪个输入实际上是范围内的焦点元素。它实际上也只执行一次。然后它完全因为解绑而停止工作。我明白了。。。在我测试它的时候,它似乎不起作用。我明白了。。。在我测试它时,它似乎不起作用。