jquery自定义函数

来源:互联网 发布:js 产生2 8随机数 编辑:程序博客网 时间:2024/06/05 06:21


第一种普通调用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>艾它社区</title>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
 /*jquery函数*/
function fun1(){
   $("div").css("color", "red");
};
$(document).ready(function(){   
 /*jquery函数调用方式*/ 
 $("button").click(function(){
  fun1();     
 }); 
})
</script>
</head>
<body>
<button>点击我</button>
<div>我会变红的哦</div>
</body>
</html>
第二种jquery对象中的自定义函数


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>艾它社区</title>
<script type="text/javascript" src="./jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 /*jquery函数*/
 $.extend({'fun1':function(){
 $("div").css("color", "red");
 }});   
 /*jquery函数调用方式*/ 
 $("button").click(function(){
  $.fun1();     
 }); 
})
</script>
</head>
<body>
<button>点击我</button>
<div>我会变红的哦</div>
</body>
</html>