JQuery fn namespace

I am reading about the jQuery $ .fn namespace, and in order to understand what I am reading, I would like to give an example of a complete command if shortcuts were not made. For example,

$('p').click(function() { console.log('click'); }); 

Could this be rewritten with .fn in it somewhere? What will be the comprehensive syntax?

 jQuery('p',document).fn.click(function() { window.console.log('click'); }); 
+6
jquery
source share
2 answers

jQuery.fn is an alias for jQuery.prototype ; This is the standard Javascript prototype mechanism.
Actual jQuery objects (instances) do not have the fn property.

You need to call functions for which the keyword this is the object with which you call it using call or apply .

For instance:

 jQuery.fn.click.call(jQuery('p',document), function() { ... }) 
+5
source share

jQuery fn is similar to C # extension methods. For example, jQuery does not have a built-in function to switch the hide and show the element, however you can build it yourself using fn. Example:

 <head> <script src="jquery-1.4.4.js"></script> <script type="text/javascript"> $(function() { var toggle = true; $("a").click(function() { $("p.neat").showIt(toggle,"fast"); toggle = !toggle; return false; }); $.fn.showIt = function(b,param) { if (b) $(this).hide(param); else $(this).show(param); }; }); $("p.neat").hide(); </script> </head> <body> <p class="neat" style="width: 200"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. </p> <a href="/">Yeah</a> </body> 

Source: http://www.ienablemuch.com/2010/06/first-foray-to-jquery.html

+4
source share

All Articles