jquery如何調用自定義函數

第一種普通調用

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>艾它社區</title>
  6. <script type="text/javascript" src="./jquery.js"></script>
  7. <script type="text/javascript">
  8. /*jquery函數*/
  9. function fun1(){
  10.    $("div").css("color", "red"); 
  11. };
  12. $(document).ready(function(){    
  13. /*jquery函數調用方式*/  
  14. $("button").click(function(){
  15.   fun1();      
  16. });  
  17. })
  18. </script>
  19. </head>
  20. <body>
  21. <button>點擊我</button>
  22. <div>我會變紅的哦</div>
  23. </body>
  24. </html>
複製代碼

第二種jquery對象中的自定義函數

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>艾它社區</title>
  6. <script type="text/javascript" src="./jquery.js"></script>
  7. <script type="text/javascript">
  8. $(document).ready(function(){
  9. /*jquery函數*/
  10. $.extend({'fun1':function(){
  11. $("div").css("color", "red"); 
  12. }});    
  13. /*jquery函數調用方式*/  
  14. $("button").click(function(){
  15.   $.fun1();      
  16. });  
  17. })
  18. </script>
  19. </head>
  20. <body>
  21. <button>點擊我</button>
  22. <div>我會變紅的哦</div>
  23. </body>
  24. </html>
複製代碼
注:
1.運行代碼時,要有jquery.js文件,否則運行出錯

2.還有其它方法,本人只測試了這兩種,不當之處,請多指教!

出處:http://bbs.it985.com/thread-6085-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章