jquery 函数中的 this.element 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9075943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:17:47  来源:igfitidea点击:

what is this.element in a jquery function?

jquery

提问by esther h

I was reading an excellent explanation of the jQuery UI combobox by J?rn Zaefferer (here's the link).

我正在阅读 J?rn Zaefferer 对 jQuery UI 组合框的出色解释(这是链接)。

The fourth line of code reads var select = this.element.hide()

第四行代码读取 var select = this.element.hide()

J?rn says:

J?rn 说:

The var select references the select element on which the combobox gets applied. To replace the select with the text input, the select is hidden.

var select 引用应用组合框的 select 元素。要将选择替换为文本输入,选择是隐藏的。

I am learning jQuery now, and I don't recall seeing this.elementbefore. How is it different that just this?

我现在正在学习 jQuery,我不记得以前看过this.element。只是这个有什么不同?

回答by Dima Koderov

Inside a widget, "this" refers to the widget object itself, which contains a property "element". That "element" points to html element for which that widget has been applied.

在小部件内部,“this”指的是小部件对象本身,它包含一个属性“元素”。该“元素”指向应用了该小部件的 html 元素。

回答by Rezigned

You can think of it like this.

你可以这样想。

this.element // is just normal jquery object

// for example
var element = $('.current-selected-dropdown');

// and then put this together inside ui object
this.element = element

I'm not sure if this would help you.

我不确定这是否对你有帮助。

var Dropdown = {
    element: null,
    _init: function() {

        // here is the same this.element that you referred to. 
        this.element = $('.dropdown');
    }
}

回答by Amit

The this being referred here is probably not the query object, and this.element has been used to cache the query object.

这里引用的this可能不是查询对象,this.element已经被用来缓存查询对象了。