jQuery之插件开发--jQuery.fn.extend()

本文档演示了如何使用jQuery的扩展方法来创建一个简单的插件,该插件包括按钮点击事件及弹窗对话功能。通过自定义对话框让用户输入ID,并将输入的ID显示在页面上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test</title>
    <script src="jquery.js"></script>
    <script src="jqui/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="jqui/jquery-ui.css">
    <style type="text/css">
        .dialog{
            display: none;
        }
    </style>

    <script type="text/javascript">
        (function($){
            $.fn.extend({
                test:function(){
                    this.click(function(){
                        alert($(this).val())
                    });
                },
                dialogTest:function(){
                    var _this = this;
                    $('.dialog').dialog({
                        modal:true,
                        buttons:{
                            确定:function(){
                                rs = $('input[name=id]').val();
                                _this.html(rs);
                                $(this).dialog('close');
                            }
                        }
                    });
                    return this;
                }
            });
        })(jQuery);
    </script>
</head>
<body>
    <div class="show"></div>
    <button id="me">Click Me</button>
    <div class="dialog">
        <input type="text" name="id" placeholder="请输入ID">
    </div>


    <script type="text/javascript">
        $('body').on('click', '#me', function(){
            $('.show').dialogTest();
        });
    </script>
</body>
</html>

jQuery.fn.extend 可以写扩展插件
以上为Demo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值