jquery----语法扩展(导入js文件)

摘要:
步骤1:创建一个新的js文件。步骤2:添加$extend;第三步是使用$。GDP(),以html;;练习---t Js文件//匿名函数;tjshtml作业1容器{margin-top:50px;}用户名“helpBlock”class=“help block”˃密码提交$。myValidate()˂!

简单使用

第一步,新建js文件

第二步,在js文件中添加

$.extend({
      "GDP": function () {
         console.log("哈哈哈哈");
      }
   });

第三步,在html中使用

  $.GDP()  即可

复杂(1,希望一些函数不可以被外部引用,不可以被修改$)

(function (jq) {
    jq.extend({
       "GDP":function () {
           foo();          //调用内部函数
          console.log("带小红花")
       },   //可以扩展多个(加上逗号在写几个)
        "SGS":function () {
          console.log("你蛤蟆")
    }
});
    function foo() {
        console.log("英语八级就是牛")
    }
})(jQuery);
(function (jq) {
    jq.fn.extend({
    "BJG":function () {
        foo2();
        console.log("就这样吧")
    }
});
    function foo2() {
        console.log("哎哎呀")
    }
})(jQuery);

练习----t.js 文件

jquery----语法扩展(导入js文件)第1张jquery----语法扩展(导入js文件)第2张
// 匿名函数
(function (jq) {  //jq就相当于$
    jq.extend({
        "myValidate": function (form) {
            var formObj = jq(form) ;//赋一个变量,因为我们经常用到
            formObj.find("button").on("click", function () {
                console.log("-------------")
                formObj.find(".form-group").removeClass("has-error");
                formObj.find("span").text("");
                formObj.find(":input").each(function () {
                    if ($(this).val().length === 0) {
                        console.log("111")
                        var name = $(this).prev().text();
                        $(this).parent().addClass("has-error");
                        $(this).next().text(name + "不能为空");
                        return false
                    }
                });
                return false
            });
        }
    })
})(jQuery);
tjs

html

jquery----语法扩展(导入js文件)第1张jquery----语法扩展(导入js文件)第4张
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>作业1</title>
    <script src="jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css">
    <style>
        .container {
            margin-top: 50px;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-4 col-md-offset-4">
            <form action="#"  novalidate id="login">
                <div class="form-group">
                    <label for="username">用户名</label>
                    <input type="text" class="form-control" id="username" placeholder="username" required="true">
                    <span id="helpBlock" class="help-block"></span>
                </div>
                <div class="form-group">
                    <label for="Password">密码</label>
                    <input type="password" class="form-control" id="Password" placeholder="Password">
                    <span id="helpBlock2" class="help-block"></span>
                </div>
                <button type="submit" class="btn btn-default submit">提交</button>
            </form>
        </div>
    </div>
</div>

<script src="t.js"></script>
<script>
    $.myValidate()
</script>
<!--<script>-->
    <!--$(".submit").on("click",function () {-->
        <!--$("form .form-group").removeClass("has-error");-->
        <!--$("form span").text("");-->
        <!--$(":input").each(function () {-->
            <!--if ($(this).val().length===0){-->
                <!--var name =  $(this).prev().text();-->
                <!--$(this).parent().addClass("has-error");-->
                <!--$(this).next().text(name+"不能为空");-->
                <!--return false-->
            <!--}-->
        <!--});-->
    <!--return false-->
    <!--})-->


<!--</script>-->
</body>
</html>
html

免责声明:文章转载自《jquery----语法扩展(导入js文件)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇持久化存储解决方案MagicalRecord使用面试分享:一年经验初探阿里巴巴前端社招下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

后端服务性能压测实践

转自:https://mp.weixin.qq.com/s/XW9geHZ9odHdI7srDiKBIg 目录 背景 环境检测 压力机及压力工具检测 Linux openfiles limit 设置 排查周边依赖 空接口压测检测 聚合报告中 throughput 计算 压测及性能排查方法 关注各纬度 log Linux 常规命令 性能排查两种方式(...

babel.config.js 和 .babelrc 对比

Babel 有两种并行的配置文件格式,可以一起使用,也可以分开使用。 项目范围的配置 babel.config.js 文件,具有不同的拓展名(json、js、html)babel.config.js 是按照 commonjs 导出对象,可以写js的逻辑。 相对文件的配置 .babelrc 文件,具有不同的拓展名 总结:baberc 的加载规则是按目录加载的...

更新js缓存办法

http://zhenggm.iteye.com/blog/680600 遇到的问题:在访问量比较大的系统中,我们需要将一些静态的文件在客户端缓存,以减少下载的流量,从而加快客户端访问的速度。但是缓存会带来一个问题,就是如何在服务端更新完文件之后,客户端能够及时得到更新。 解决思路:1. 在一个配置文件中维护js文件map; 2. 在页面中通过js ma...

JS字符编码----ASCII,Unicode 和 UTF-8

转:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html 1.ASCII 码 八个二进制位就可以组合出256种状态,这被称为一个字节(byte) 使用7 位二进制数(剩下的1位二进制为0)来表示所有的大写和小写字母,数字0 到9、标点符号, 以及在美式英语中使用的特殊控制字符...

Centos 6/Redhat 6:远程图形桌面: tigervnc

step 0:【Centos 6/Redhat 6】安装桌面环境。 step 1:安装tigervnc-server step 2:配置 /etc/sysconfig/vncservers     样例: VNCSERVERS="1:root 2:oracle 3:mysql" VNCSERVERARGS[1]="-geometry 1024x768"...

Jquery UI Tabs 获取选中Tab的ID

<button>get current</button>current tab: <span id="current"></span> <div id="tabs"> <ul> <li><a href="#tabs-1">Nunc ti...