zoukankan      html  css  js  c++  java
  • jquery extend扩展方法(类方法和对象方法)

    一、扩展jquery类方法

    /**
    * 扩展jquery类方法(相当于类的静态方法$.methodName(param))
    */

    $.extend({
    	con:function(value){
    	  console.log(value)
    	}
    })
    //类方法调用
    $.con('作者是包戬作者是包戬作者是包戬作者是包戬作者是包戬作者是包戬');
    

      

    二、扩展jquery对象方法

    /**
    * 扩展jquery对象方法(相当于类的静态方法$('#id').methodName(param))
    * 通过id/class/标签名得到的是对象,直接$.方法名的是类(假定意义上的类)
    * jQuery.fn = jQuery.prototype = {//这就是在实例化一个jquery对象
    * init: function( selector, context ) {//….
    * //……
    * };
    */

    //第一类定义jquery方法(如果自定义方法过多实用)
    $.fn.extend({
      myPlugin:myPlugin
    });
    function myPlugin(options) {
      $options = $.extend({ 
        html: "no messages", 
        css: { 
          "color": "red", 
          "font-size":"14px"
        }
      },options); 
      return $(this).css($options.css).html($options.html); 
    } 
    //或者
    $.fn.extend({
    myPlugin:function(options) {
    	$options = $.extend({ 
    	html: "no messages", 
    	css: { 
    		"color": "red", 
    		"font-size":"14px"
    	}
    	},options); 
    	return $(this).css($options.css).html($options.html); 
    } 
    }); //第二类定义jquery方法(定义单个方法),两类其实都是一样的原理,分成两类也是我自己定的,有错勿喷 $.fn.myPlugin = function(options) {   $options = $.extend({     html: "no messages",     css: {       "color": "red",       "font-size":"14px"     }   },options);   return $(this).css($options.css).html($options.html); } //调用 $('.ye').myPlugin({html:"So easy,yes?",css:{"color":"green","font-size":"50px"}}); //页面上标签<span class='.ye'><span>

      

  • 相关阅读:
    leetcode18
    CSS 1. 选择器
    HTML
    练习题|MySQL
    练习题||并发编程
    第八章| 3. MyAQL数据库|Navicat工具与pymysql模块 | 内置功能 | 索引原理
    mysql练习
    第八章| 2. MySQL数据库|数据操作| 权限管理
    第八章| 1. MySQL数据库|库操作|表操作
    第七章|7.4并发编程| I/O模型
  • 原文地址:https://www.cnblogs.com/jamsbwo/p/5360088.html
Copyright © 2011-2022 走看看