zoukankan      html  css  js  c++  java
  • jQuery $.extend()使用方法

    $.extend()使用方法总结。
    jQuery为开发插件提拱了两个方法,各自是:
    jQuery.fn.extend(object);

    jQuery.extend(object);  

    jQuery.extend(object);为扩展jQuery类本身.为类加入新的方法。
    jQuery.fn.extend(object);给jQuery对象加入方法。

    这个应该非常好理解吧。

    举个样例。

    <span style="font-size:18px;"><html>
    <head>
    	<title></title>
    </head>
    <body>
    <h3 class="ye">new soul</h3>
    <h3 class="ye">new soul</h3>
    <h3 class="ye">new soul</h3>
    <h3 class="ye">new soul</h3>
    <script type="text/javascript" 	src="jquery.2.0.3.js"></script>
    <script type="text/javascript">
    jQuery.fn.myPlugin = function(options) {
        $options = $.extend( {
                html: "no messages",
                css: {
                    "color": "red",
                    "font-size":"14px"
                }},
                options);
            return $(this).css({
                "color": $options.css.color,
     
            }).html($options.html);
        }
     
    
    $('.ye').myPlugin({html:"So easy,yes?

    ",css:{"color":"green","font-size":"20px"}}); </script> </body> </html> </span>


    好的,上面你也看到了一点点$.extend()的使用方法。


    1.合并多个对象。


    这里使用的就是$.extend()的嵌套多个对象的功能。

    所谓嵌套多个对象,有点类似于数组的合并的操作。

    可是这里是对象。举例说明。

    <span style="font-size:18px;">//使用方法: jQuery.extend(obj1,obj2,obj3,..)
    var Css1={size: "10px",style: "oblique"}
    var Css2={size: "12px",style: "oblique",weight: "bolder"}
    $.jQuery.extend(Css1,Css2)
    //结果:Css1的size属性被覆盖,并且继承了Css2的weight属性
    // Css1 = {size: "12px",style: "oblique",weight: "bolder"}
    </span>


    2.深度嵌套对象。


    <span style="font-size:18px;">  jQuery.extend(   
        { name: “John”, location: { city: “Boston” } },   
        { last: “Resig”, location: { state: “MA” } }   
      );   
       // 结果:   
       // => { name: “John”, last: “Resig”, location: { state: “MA” } }
     // 新的更深入的 .extend()   
      jQuery.extend( true,   
      { name: “John”, location: { city: “Boston” } },   
        { last: “Resig”, location: { state: “MA” } }   
     );   
     // 结果   
      // => { name: “John”, last: “Resig”,   
     //      location: { city: “Boston”, state: “MA” } }  
    </span>

    3.能够给jQuery加入静态方法。


    <span style="font-size:18px;"><html>
    <head>
    	<title></title>
    </head>
    <body>
    <script type="text/javascript" 	src="jquery.2.0.3.js"></script>
    <script type="text/javascript">
    	$.extend({
    		add:function(a,b){return a+b;},
    		minus:function(a,b){return a-b},
    		multiply:function(a,b){return a*b;},
    		divide:function(a,b){return Math.floor(a/b);}
    	});
    	
    	var sum = $.add(3,5)+$.minus(3,5)+$.multiply(3,5)+$.divide(5,7);
    	console.log(sum);
    </script>
    </body>
    </html></span>
    Best Wishes

  • 相关阅读:
    POJ 3710 Christmas Game#经典图SG博弈
    POJ 2599 A funny game#树形SG(DFS实现)
    POJ 2425 A Chess Game#树形SG
    LeetCode Array Easy 122. Best Time to Buy and Sell Stock II
    LeetCode Array Easy121. Best Time to Buy and Sell Stock
    LeetCode Array Easy 119. Pascal's Triangle II
    LeetCode Array Easy 118. Pascal's Triangle
    LeetCode Array Easy 88. Merge Sorted Array
    ASP.NET MVC 学习笔记之 MVC + EF中的EO DTO ViewModel
    ASP.NET MVC 学习笔记之面向切面编程与过滤器
  • 原文地址:https://www.cnblogs.com/jhcelue/p/6728059.html
Copyright © 2011-2022 走看看