jquery plugin - how to make $(this) working in the context of a custom setting -
below simplified version of idea of want do.
i trying set custom option below how use $(this) in wring context. how write main object gets used , not in function, right fnode error.
beforedothis: function () { $(this).css({color:'blue'}); alert($(this).text()); }
html
<div> <div id="one">test1</div> <br /> <br /> <br /> <div id="two">test2</div> <br /> <br /> <br /> <div id="three">test3</div> </div>
javascript
/* moved own custom js plugin file ------------------------------------------- */ //custom plugin $.fn.addmyspan = function (customoptions) { var defaults = { textcolor: "#033", beforedothis: function () {} }; var settings = $.extend({}, defaults, customoptions); return this.each(function () { settings.beforedothis(); }); }; /* -----------------------------------------------------------------------------------------------------*/ $(document).ready(function () { $('#one,#two,#three').addmyspan({ textcolor: "red", beforedothis: function () { $(this).css({color:'blue'}); alert($(this).text()); } }); });
store reference this
set function's context call
:
var self = this; var defaults = { textcolor: "#033", beforedothis: function () {} }; var settings = $.extend({}, defaults, customoptions); return this.each(function () { settings.beforedothis.call(self); });
Comments
Post a Comment