zoukankan      html  css  js  c++  java
  • JQuery Plugin 1

    1. How do you write a plugin in jQuery?

    You can extend the existing jQuery object by writing either methods or functions.

    1) Writing a custom jQuery method

    jQuery methods are defined by extending the jQuery.fn object with your method name.

    $.fn.extend({
        //Only Test
        MasterTest: {
            alertTest: function () {
                alert('Welcome Master HaKu!');
            }
        }
    });

    call the method

    $(document).ready(function () {
            $('#btnAlert').click(function () {
                $.fn.MasterTest.alertTest();
            });
        });


    eg:

    /*
        JQuery Plugin - custom jQuery method
    */
    
    $.fn.appendHtml = function (destId, appendId) {
        $(destId).append('<div>Add Text: ' + $(appendId).html() + '</div>');
    };

    call the method

    $.fn.appendHtml('#result', '#myText');


    2) Writing a custom jQuery function

    eg:

    /*
        JQuery Plugin - custom jQuery function
    */
    
    $.appendHtml = function(destId, appendId) {
        $(destId).append('<div>Append: ' + $(appendId).html() + '</div>');
    };


    call the function

    $.appendHtml('#result', '#myText');
  • 相关阅读:
    暑假第二十七测
    暑假第二十七测
    【真题解】牛宫
    【伪题解】牛宫
    最优贸易
    跳马问题
    求和问题
    【题解】山区建小学
    OpenStack之虚机冷迁移代码简析
    OpenStack之虚机热迁移代码解析
  • 原文地址:https://www.cnblogs.com/davidgu/p/3331537.html
Copyright © 2011-2022 走看看