Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Reference current context when setting jQuery plugin options
- (function ($) {
- $.fn.colourise = function (options) {
- var settings = $.extend({
- color: "black"
- }, options);
- return this.each(function () {
- $(this).css("color", options.color);
- });
- };
- })(jQuery);
- <div data-color="red">
- This text should be red.
- </div>
- <div data-color="blue">
- This text should be blue
- </div>
- <div data-color="green">
- This text should be green
- </div>
- $(function () {
- // This feels a bit wrong to have to use a .each() here, but how else do we do it?
- $("div").each(function () {
- $(this).colourise({
- color: $(this).data("color")
- });
- });
- });
- $(function () {
- $("div").colourise({
- color: [get context of this "div" somehow].data("color")
- });
- });
- function getColor(){
- return $.data( this, "color" );
- }
- $(function () {
- $("div").colourise({
- color: getColor
- });
- });
- (function ($) {
- $.fn.colourise = function (options) {
- var settings = $.extend({
- color: "black"
- }, options);
- return this.each(function () {
- var color = options.color;
- color = typeof color =="function" ? color.call( this ) : color;
- $(this).css("color", color);
- });
- };
- })(jQuery);
Add Comment
Please, Sign In to add comment