Jquery学习笔记

1.提交表单时,如果需要阅读协议,必须要阻止表单提交,阻止表单提交,可以这么做、

1         $('a').click(function (e) {
2           e.preventDefault();//阻止事件默认函数
3           //prevent 阻止,阻挠、
4         });

ps:html代码如下显示:

1 <a id="id" href="http://baidu.com/">百度一下</a>

2.html元素页面垂直居中设置

1         jQuery.fn.center = function () {
2               this.css("position","absolute");
3               this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
4               this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
5               return this;
6          }
7          
8          $("#id").center();
9         

3.Jquery 插件开发

  a.类级别 拓展jquery类,jQuery.extend(object); 为jQuery类添加类方法,可以理解为添加静态方法。如:

  

1     $.extend({
2         add:function(a,b){ return a+b;},
3         minus:function(a,b){ return a-b;}    
4     });
5     
6     var i = $.add(1,2);
7     var j = $.minus(4,3);
8     console.log(i);
9     console.log(j);

  

 1 var obj1 = {
 2             apple:0,
 3             banan:{price:100,total:200}
 4         }
 5     var obj2 = {
 6             xxx:1,
 7             banan:{price:22,total:22}
 8 
 9         }
10     $.extend(obj1,obj2);
11     var xx = obj1.banan.price;
12     console.log("xxxxx"+xx);

  b.jQuery.fn.extend(object); 对jQuery.prototype进得扩展,就是为jQuery类添加“成员函数”。jQuery类的实例可以使用这个“成员函数”。我们要开发一个插件,做一个特殊的编辑框,当它被点击时,便alert 当前编辑框里的内容。可以这么做:

1     $.fn.extend({
2         alertWhileClick:function(){
3             $(this).click(function(){
4                 alert($(this).val());
5             });
6         }
7     });
1         <input id="input" value="牛逼" type="text">
2         <script>
3             $("#input").alertWhileClick();
4         </script>
本文欢迎转载,转载请注明出处,如果涉及侵权,请企鹅:723887809。
原文地址:https://www.cnblogs.com/toxufe/p/5566145.html