首先:$.extend({})
用这个方法给jquery本身增加一个hello的方法;
$(document).ready(function (){
$.extend({
hello:function(){alert("hello world!")},
});
$("p").click(function(){
$.hello();
})
})
</script>
ps:给jquery新建了一个$.hello(),然后绑定给p元素一个点击事件,点击p元素就可以弹出对话框,hello world! 这只是一个简单的例子还可以去定义更多的静态方法给jquery
其次:$.fn.extend({})
<script type="text/javascript">
$(document).ready(function (){
$.fn.change = function(){
$(this).css("background","red");
};
$("p").click(function (){
$(this).change();
})
})
</script>
ps:首先用(this).change(),单击p元素是背景颜色会变成红色。
可以简单的对比一下,这两种方法的区别到底在哪里:
第一种.extend()方法,使用时需要带着jq的标注&.hello() ;而第二种(this).change();
第一种方法是扩展一个jq方法(只是为jq添加一个方法),第二种是为jq的对象扩展一个方法(对Dom对象的操作);