Sunday, July 29, 2012

jquery constructor with context $(selector,this)

Inside the jquery event handler, quite often we need to select a sub element within the passed in event element, and do some operation on it. The passed in event element is accessible from the this object. That is the place you will see a lot of $("selector", this) get called.

$(selector, this) is another jquery constructor, the second parameter takes a context variable. In this case, instead of selecting the elements from the current document, it just selects the matched elements from the context parameter. In the event handler, "this" represents the DOM object that the current event happens on it, to use the jquery method on it, we need first converts it to jquery object with $(this).

Wednesday, July 18, 2012

Common jquery shortcut

$(...)
=>
jQuery()

All jquery methods are started by calling jQuery(), which can be written as $(...)
 


$(function(){...});
=> 
$(document).ready(function(){...});
The function will be called when document is ready.
Do not mix this shortcut with javascript self-invoked anonymous function, the ending () makes the js function to be invoked, and thbe leading () indicates this is not normal function.
(function(){
...
})();
 

$.fn.myFeature = function () { ... } 
=>
jQuery.prototype.myFeature = function () { ... } 
 
The function to create a jquery plugin by adding method into p