tailieunhanh - JQuery: Novice to Ninja- P25

JQuery: Novice to Ninja- P25:No matter what kind of ninja you are—a cooking ninja, a corporate lawyer ninja, or an actual ninja ninja—virtuosity lies in first mastering the basic tools of the trade. Once conquered, it’s then up to the full-fledged ninja to apply that knowledge in creative and inventive ways. | Plugins Themes and Advanced Topics 337 chapter_09 01_plugins excerpt p .hide .highlightOnce .slideDown It s quite exciting our functionality is captured in a chainable reusable plugin that we ve nestled in between the hide and slideDown actions. Seeing how 11 lines of code was all that was required and six of those are stock-standard plugin scaffolding you can see it s worth turning any functionality you intend on reusing into a plugin Adding Options jQuery plugins are an excellent way to produce reusable code but to be truly useful our plugins need to be applicable outside the context for which we created them they need to be customizable. We can add user-specified options to our plugins which can then be used to modify the plugin s behavior when it s put to use. We re familiar with how options work from a plugin user s perspective as we ve passed in options to just about every plugin we ve used throughout the book. Options let us modify the plugin s functionality in both subtle and more obvious ways so that it can be used in as wide a range of situations as we can imagine. There are two types of plugin options simple values and object literals. Let s start with the simpler one to see how this works. For our highlightOnce plugin it seems quite limiting to have the color hard-coded. We d like to give developers the choice to highlight their elements in any color they d like. Let s make that an option chapter_09 02_plugin_options excerpt . function color this .css background-color color fff47f The plugin can be called with a color but can also be called without parameters in which case a default value will be used thanks to the JavaScript operator . Let s highlight our paragraphs in green 338 jQuery Novice to Ninja chapter_09 02_plugin_options excerpt p .hide .highlightOnGe green .slideDown If you have one or two simple options that are always required this approach is fine. But when your plugins start to .