Write a plugin that sets and gets the color of the element:
First implement the first function, set:
;(function($) { $.fn.extend ({ color:function(value) { return this. css ("Color", value); the//css method itself is the jquery object that returns the current element } ); }) (JQuery);
$ ("span"). Color ("red"); //Call
Then implement the second function, get: If the value is not passed
;(function ($) {$.fn.extend ({ Color: function (value) { if (value = = ' undefined ' ) {//If the value is not passed in is get return this . CSS ("color" ); else { return ,value); //css method itself has the ability to return the style value of the first matching element, where it is not necessary to get the first element by EQ () } } }); }) (jQuery);
In fact, there is a mechanism inside the CSS method that determines whether value is undefined, so it is possible to return different values based on the difference in the passed parameters. So you can:
;(function($) { $.fn.extend ({ color:function(value) { return this. css ("color"//Pass value is set, no pass is value } } ) (jQuery);
Sharp jquery-7--The writing process of a $.fn.color plugin