代码之家  ›  专栏  ›  技术社区  ›  Remotec

JQuery将事件附加到实例中的方法

  •  0
  • Remotec  · 技术社区  · 14 年前

    我有这样的代码。。。。

    function Finder(id) {
                this.id = id;
                this.input = $("#" + this.id + " :input[type='text']:first")[0];
                $(this.input).bind('keyup'....);
    
    
    
                this.KeyUpHandler = function (e) { ..the event should trigger this.. }
    

    this.input=在'id'中找到的input类型的第一个元素,我将引用它。这对我的需要很管用。

    然后我要做的是在“input”上绑定keyup事件。但是,我希望事件引用函数this.KeyUpHandler()中包含的实例方法。

    另外,如果我刚刚对输入的标记(onkeypress=“keyuphandler();”)执行了此操作,则需要将“e”作为传递到函数中的事件。

    有什么想法可以将事件绑定到我正在工作的函数实例中的a函数吗?

    2 回复  |  直到 14 年前
        1
  •  3
  •   thejh    14 年前
    function Finder(id) {
      this.id = id;
      this.input = $("#" + this.id + " :input[type='text']:first")[0];
      that=this;
      this.KeyUpHandler = function (e) { ..the event should trigger this.. }
      $(this.input).bind('keyup', this.KeyUpHandler);
    }
    

    你打电话来很重要 bind() 之后 定义函数!

        2
  •  0
  •   Ali Tarhini    14 年前
      this.KeyUpHandler = function (e) { ..the event should trigger this.. } 
         $(this.input).bind('keyup', function(){
              this.KeyUpHandler(event);
             //more code can go here
          });