2012-01-31 27 views
9

我正在閱讀JörnZaefferer(here's the link)對jQuery UI組合框的極好的解釋。jquery函數中的this.element是什麼?

的代碼第四行讀取var select = this.element.hide()

JORN說:

的VAR選擇引用在其上被施加的組合框選擇元件。要用文本輸入替換選擇,選擇是隱藏的。

我正在學習jQuery,現在我不記得看到this.element之前。它與這個有何不同?

+0

...並且,元素名稱? '$(X).html()'顯示元素的內容,但不顯示元素本身! – 2013-02-15 03:14:25

+0

這裏:'var elementName = $(this).get(0).tagName.toLowerCase();' – 2013-02-15 03:23:26

回答

11

在小部件內部,「this」指的是小部件對象本身,它包含一個屬性「element」。該「元素」指向已應用該小部件的html元素。

+0

謝謝,我認爲這可能是這種情況。所以一個小部件本身就是一個對象。與插件相反,「this」指的是您要調用的對象。 – 2012-01-31 07:54:26

2

這裏提到的這個可能不是查詢對象,並且this.element已經被用來緩存查詢對象。

7

你可以這樣想。

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 

我不確定這是否會對您有所幫助。

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

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

是的,這有助於這個概念,謝謝 – 2012-01-31 07:56:49

相關問題