jQuery中this和$(this)有什麼區別?我應該什麼時候使用哪個?
3
A
回答
3
$('p').each(function() {
//this.id;
//$(this).attr('id');
})
如果你認爲上面的jQuery的功能將通過頁面上的每個段落元素循環,將通過傳遞「這個」變量到匿名函數返回的每個段落元素的引用。如果'this'變量包含在jQuery函數($(this))中,那麼我們就可以訪問與元素相關的所有jQuery善良,例如$(this).find('span')
。它自己的'this'對象只是一個普通的Javscript DOM對象。
0
this
返回一個本地JavaScript對象(如果我稱它爲正確的話),$(this)
返回一個jQuery對象。
$(this)[0] == this
0
$()是jQuery的構造函數。
這是對調用DOM元素的引用。
所以基本上,在$(this)中,您只是將這個作爲參數傳遞給$(),以便您可以調用jQuery方法和函數。
重複:jQuery: What's the difference between '$(this)' and 'this'?
的相關問題
- 1. jQuery:'$(this)'和'this'有什麼區別?
- 2. 「this」,「$ this」和「$(this)」之間的區別是什麼?
- 3. $,this和$(this)在JavaScript基礎知識中有什麼區別?
- 4. 'this'和'getActivity()'有什麼區別?
- 5. synchronized(this)和synchronized(ClassName.class)有什麼區別?
- 6. $(this)和$(「#id」)有什麼區別?
- 7. `$ this-> name`和`$ this - > $ name`之間有什麼區別?
- 8. $ this - > {$ spec}和$ this - > $ spec有什麼區別?
- 9. $(this)和jQuery中的這個有什麼區別?
- 10. $ this-> render()和$ this-> redirect()之間的區別是什麼
- 11. 「$(this)」和「this」之間的區別是什麼?
- 12. $ this-> requestElement()和$ this-> element()之間的區別是什麼?
- 13. IIFE:var vs this - 有什麼區別嗎?
- 14. Ember中的set(this,'agentName','John')和this.set('agentName','John')有什麼區別?
- 15. jquery this和$(this)
- 16. 在JavaScript中使用「this」和「prototype」有什麼區別嗎?
- 17. 調用$ CI和在codeigniter中使用`this`有什麼區別嗎?
- 18. 在Node.js模塊中使用「exports」和「this」有什麼區別?
- 19. ColdFusion中THIS範圍和APPLICATION範圍有什麼區別?
- 20. 函數中`this`和`var`有什麼區別?
- 21. 在Javascript中使用var和this有什麼區別?
- 22. `this instanceof String`和``foo「instanceof String`有什麼區別?
- 23. 使用「this」指針和不使用它有什麼區別?
- 24. $(e.currentTarget)和$(this)之間是否有區別?
- 25. 在這些例子中調用'this'有什麼區別?
- 26. $ this-> id和$ id的區別是什麼
- 27. self.element ='this'和self.send(「element =」,'this')之間的區別
- 28. $ this-> params ['url']和$ this-> params ['named']之間的區別
- 29. bind和var self之間的區別= this?
- 30. 這和.this之間的區別?
可能重複[jQuery的$(這)對這個(http://stackoverflow.com/questions/1051782/jquery-this-vs-this) –