Does 'this' refer to the element that called this function?

主宰稳场 提交于 2019-12-02 02:24:28

jQuery sets this itself, generally to point to the current element.

Otherwise, in JavaScript...

As a function assigned to a propety

var a = {
   b: function() {
      // `this` is `a`
   }
}

a.b();

Except, where the property becomes assigned to a variable. Observe...

var c = a.b;
c(); // the `this` will point to `window`

As a function assigned to a variable

var a = function() {
   // `this` is `window`
}

a();

As a Constructor

var C = function() {
   // `this` is `c`
}

var c = new C();

Note that if your forgot to instantiate with new, JavaScript will assign those properties to the global object (window in a browser).


In the global scope

// In global scope `this` is `window`
var d = this;

As called with call() or apply()

You can also set this explicitly with call() and apply() function methods.


Now, to your problem...

Thanks for the explanation of 'this' but I still dont understand why the reference isn't working in my code

My apologies.

$(this).name won't work as this is now a jQuery object and has only a few properties (none of which are name).

Either use $(this).attr('name') or drop wrapping this with the jQuery object and just access this.name.

That's what the obj parameter is for in the onSelect function, so you'd reference $(obj). Just for reference, this will refer to the associated input field.

For reference, see: http://jqueryui.com/demos/datepicker/ > Events > onSelect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!