jQuery.extend与jQuery.fn.extend的区别分析与实例
jQuery.extend与jQuery.fn.extend都是为jquery开发插件。
不同是的:
jQuery.extend(object); 为扩展jQuery类本身.为类添加新的方法。
jQuery.fn.extend(object);给jQuery对象添加方法。
前者不需要创建jquery实例就可以执行。
后者必须得对象绑定事件才可以执行!
代码如下:
<script language="javascript" src="http://www.phplike.com/javascript/jquery-1.4.2.js"></script>
<script language=javascript>
jQuery.extend({
merge:function(a,b){
return a + " " + b;
}
});
alert($.merge('Hello','World'));
</script>
后者必须绑定对象的事件:
<input name="phplike" type="text" />
<script language="javascript" src="http://www.phplike.com/javascript/jquery-1.4.2.js"></script>
<input name="phplike" type="button" value="Click Here" />
<script language="javascript">
jQuery.fn.extend({
merge:function(a,b){
return a + " " + b;
}
});
$("input[name='phplike']").click(function (){
alert($(this).merge('Hello','World'));
});
</script>
转载于:https://www.cnblogs.com/ninelands/archive/2012/11/07/2758762.html
创作挑战赛新人创作奖励来咯,坚持创作打卡瓜分现金大奖总结
以上是生活随笔为你收集整理的jQuery.extend与jQuery.fn.extend的区别分析与实例的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Eclipse设定文件的默认打开方式
- 下一篇: CXF小窥:知道服务器端wsdl地址,如