您的位置:首页 > Web前端 > JQuery

jquery如何调用自定义函数

2014-09-24 20:07 337 查看
原地址 : http://bbs.it985.com/thread-6085-1-1.html

第一种普通调用

<!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函数*/
<strong><span style="color:#3333FF;">function fun1(){
$("div").css("color", "red");
};
$(document).ready(function(){
/*jquery函数调用方式*/
$("button").click(function(){
fun1();
});
})</span></strong>
</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">
<strong><span style="color:#3333FF;">$(document).ready(function(){
/*jquery函数*/
$.extend({'fun1':function(){
$("div").css("color", "red");
}});
/*jquery函数调用方式*/
$("button").click(function(){
$.fun1();
});
})</span></strong>
</script>
</head>
<body>
<button>点击我</button>
<div>我会变红的哦</div>
</body>
</html>


注:

1.运行代码时,要有jquery.js文件,否则运行出错

2.还有其它方法,请多指教!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: