Sharp jquery-7--The writing process of a $.fn.color plugin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.