jQuery的扩展方法有两种方式:
1)jQuery本身的扩展方法:$.extend;
2)jQuery所选对象的方法:$.fn.extend;
第二种更符合使用jQuery的习惯。
示例:
<!DOCTYPE html>
<html>
<head lang="zh-CN">
<meta charset="UTF-8">
<title>jQuery扩展</title>
<script src="js/jquery-1.11.2.min.js"></script>
<script src="js/myJQuery.js"></script>
<script src="js/extends.js"></script>
</head>
<body>
<div></div>
</body>
</html>myJQuery.js的代码:
/*第一种扩展方式*/
$.myjq=function(){
alert("这是我定义的方法");
}
/*第二种扩展方式*/
$.fn.myjq2=function(){
$(this).text("嘿!我是jason!")
}
extends.js的代码:
$(document).ready(function(){
//$.myjq();
$("div").myjq2();
})打开网页可在div内显示"嘿!我是jason!"。
本文介绍了jQuery的两种扩展方法:通过$.extend对jQuery本身进行扩展,以及通过$.fn.extend对所选对象进行扩展。并提供了具体的代码示例,展示如何利用这两种方式定义新的jQuery方法。
192

被折叠的 条评论
为什么被折叠?



