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!"。