With the new eyePhone, you can watch, listen, ignore your friends, stalk your ex, download porno on a crowded bus, even check your email while getting hit by a train.
<!doctype html>
<html lang="en">
<head>
<title>jQuery Plugins - 01</title>
</head>
<body>
<h1>jQuery.reverseText</h1>
<p>Hi, I will be reversed!</p>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.reversetext.js"></script>
<script>
$(document).ready(function() {
$('p').reverseText();
});
</script>
</body>
</html>
$.trim()
$.randomColor()
to get a random color returned.$(':lists')
to select all <ul>
, <ol>
and <dl>
elements$(':inview')
to select all elements inside the current viewport$(':external')
to select all links that link to external websites$(els).slideAndRemove();
$(els).reverseText();
alt
attribute with $('img[alt]').addDescription();
Ajax
& Dimensions
) and were later on moved into jQuery itself
Yeah, well... I'm gonna go build my own theme park, with blackjack and hookers. In fact, forget the park!
jQuery
$
for convenience
// These two have the same result
var p1 = $('p'),
p2 = jQuery('p');
.slideUp()
are defined in jQuery.prototype
jQuery.prototype
is mapped to jQuery.fn
(or $.fn
) for convenience
$
)$.expr
.prototype
($.fn
)Do refrigerators still come in cardboard boxes?
Yeah, but the rents are outrageous.
If only one argument is supplied to $.extend()
, this means the target argument was omitted. In this case, the jQuery object itself is assumed to be the target. By doing this, we can add new functions to the jQuery namespace. This can be useful for plugin authors wishing to add new methods to jQuery.
var randomColor = function() {
return '#' + Math.floor(Math.random() * 16777216).toString(16);
}
$.extend({
randomColor : function() {
return '#' + Math.floor(Math.random() * 16777216).toString(16);
}
})
$.extend
, it's possible to pass in/define multiple functions at once
$
<!doctype html>
<html lang="en">
<head>
<title>jQuery Plugins - 02</title>
</head>
<body>
<h1>jQuery.randomColor</h1>
<p>Click me to change backgroundColor</p>
<script src="js/jquery-1.7.1.min.js"></script>
<script src="js/jquery.randomcolor.js"></script>
<script>
$(document).ready(function() {
$('p').css('cursor','pointer').on('click', function(e) {
$(this).css('background', $.randomColor());
});
});
</script>
</body>
</html>
$
refers to jQuery
(function($) {
$.extend({
randomColor : function() {
return '#' + Math.floor(Math.random() * 16777216).toString(16);
}
})
})(jQuery);
Yes, I'd like to make a collect call.
jQuery.expr[':']
jQuery.extend()
to extend jQuery.expr[':']
with a new function
jQuery.extend(jQuery.expr[':'], {
example : function(elem, index, meta, stack) {
// elem - is a current DOM element
// index - the current loop index in stack
// meta - meta data about your selector
// stack - stack of all elements to loop
// Return true to include current element
// Return false to explude current element
}
});
elem
parameter
elem
is not a jQuery Instance!meta
param is an array with info about your selector, allows you to use parameters with your selector(function($) {
$.extend($.expr[':'], {
lists : function(elem, index, match, stack) {
var els = ['ul','ol','dl'];
return ($.inArray(elem.nodeName.toLowerCase(), els) > -1);
}
});
})(jQuery);
$(':lists').css('border', '1px solid red');
$('#somediv').find(':lists');
$('ul,ol,dl');
Somehow he has cobbled together a random assortment of other brain waves into a working mind.
jQuery.prototype
/$.fn
as you'd extend a regular .prototype
(function($) {
$.fn.myPlugin = function() {
// ... plugin code here
}
})(jQuery);
(function($) {
$.fn.extend({
myPlugin : function() {
// ... plugin code here
}
});
})(jQuery);
$('...').myPlugin();
this
?this
inside your plugin is a jQuery instance (in contrast to other situations where it's a DOM element)
(function($) {
$.fn.slideUpAndRemove = function() {
this.slideUp("slow", function() { // <-- jQuery Instance
$(this).remove();
});
}
})(jQuery);
(function($) {
$.fn.slideUpAndRemove = function() {
return this.slideUp("slow", function() {
$(this).remove();
});
}
})(jQuery);
(function($) {
$.fn.myPlugin = function() {
return this.each(function() {
// ... plugin code here
});
}
})(jQuery);
this
(function($) {
$.fn.showLinkLocation = function() {
return this.filter('a').each(function() {
$(this).append( ' (' + this.href + ')');
});
};
}(jQuery));
$('a').showLinkLocation().css('text-decoration','none');
(function($) {
$.fn.hilight = function(options) {
var defaults = {
foreground: 'red',
background: 'yellow'
};
// Extend our default options with those provided.
var o = $.extend({}, defaults, options);
return this.each(function() {
// ... plugin code here
});
};
}(jQuery));
defaults
outside the plugin definition, so they can be adjusted by the user
(function($) {
$.fn.hilight = function(options) {
// Extend our default options with those provided.
var o = $.extend({}, $.fn.hilight.defaults, options);
return this.each(function() {
// ... plugin code here
});
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
}(jQuery));
(function($) {
$.fn.hilight = function(options) {
var o = $.extend({}, $.fn.hilight.defaults, options);
return this.each(function() {
var $this = $(this);
$this.css({
'color': o.foreground,
'background-color': o.background
});
$this.html('<strong>' + $this.html() + '</strong>');
});
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
}(jQuery));
$('em').hilight();
$('a').hilight({foreground: '#00F'});
This is it. The moment we should've trained for.
(function($) {
$.fn.hilight = function(options) {
// ... plugin code here
};
$.fn.hilightRemove = function() {
// ... plugin code here
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
}(jQuery));
$('em').hilight();
$('em').on('click', function() {
$(this).hilight('remove');
});
(function($) {
var methods = {
init : function(options) {
// ... plugin code here
},
otherfunction : function() {
// ... plugin code here
}
};
$.fn.pluginName = function(method) {
if (methods[method]) {
return methods[method].apply(
this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.plugin');
}
};
})(jQuery);
init
will be called.(function($) {
var methods = {
init : function(options) {
var o = $.extend({}, $.fn.hilight.defaults, options);
return this.each(function() {
var $this = $(this);
$this.css({
'color': o.foreground,
'background-color': o.background
});
$this.html('<strong>' + $this.html() + '</strong>');
});
},
remove : function() {
return this.each(function(){
var $this = $(this);
$this.css({
'color': 'inherit',
'background-color': 'transparent'
});
$this.html($this.find('strong').html());
});
}
};
// ...
// ...
$.fn.hilight = function(method) {
if (methods[method]) {
return methods[method].apply(
this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || ! method) {
return methods.init.apply(this, arguments);
} else {
$.error('Method ' + method + ' does not exist on jQuery.hilight');
}
};
$.fn.hilight.defaults = {
foreground: 'red',
background: 'yellow'
};
})(jQuery);
// Add highlights
$('em').hilight();
$('a').hilight({foreground: '#00F'});
// remove highlight after 1 second
setTimeout(function() {
$('em').hilight('remove').css('border', '1px solid red');
}, 1000);
$('...').hilight('remove')
$.data