1. 程式人生 > >jQuery——插件制作

jQuery——插件制作

註冊 body pre min doctype oct style 命名空間 分享圖片

1、$.fn.extend:擴展 jQuery 元素集來提供新的方法(通常用來制作插件),使用時是$(‘選擇器‘).方法

2、$.extend:擴展jQuery對象本身,用來在jQuery命名空間上增加新函數,使用時是$.方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        div{
            width: 100px;
            height
: 100px; margin-left: 150px; margin-top: 20px; border: 1px solid #000; } </style> <script src="js/jquery.min.js"></script> </head> <body> <div></div> <script> $.fn.extend({ becomeYellow:function () {
//this是$(‘選擇器‘)獲取的jq對象,不是註冊方法中的dom對象 this.css({backgroundColor:yellow}); } }); $.extend({ becomeRed:function (a,b) { console.log(a+b); } }); $(div:eq(0)).becomeYellow(); $.becomeRed(1,2);//3 </script> </body> </html>

技術分享圖片

jQuery——插件制作