1

In jQuery, when I call:

$("selector").a_function(function(){
    this.toggleClass("a-class");
}

I am told that this is of a certain type, and does not have the jQuery function available to it. The problem is that I am not getting jQuery objects returned to me. The way I am getting around this is to do:

jquery_object = jQuery(this);

every time. I thought that $() is equivalent to calling jQuery and that I am supposed to receive a jQuery object.

Is this normal? What am I doing wrong here?

4

1 回答 1

1

你会想要使用$(this)而不是仅仅this.

在您给出的情况下,this它没有引用 DOM 元素。

通过将它包装在标准的 JQuery 选择器函数中,您实际上将调用 DOM 元素。

我经常将它用于点击处理程序。

$('a').click(function(e) { $(this).toggleClass('a-class'); });

JQuery 中几乎所有通过选择器调用并具有回调的函数都将使用这种格式。

于 2015-05-11T15:21:26.427 回答