2010-12-11 42 views
0

我是新來的JavaScript和jQuery。我想用$(document).ready來註冊一些事件處理程序。例如,像這樣的代碼,我的問題是我需要在頁面的頭部引用哪些JavaScript庫才能使用$(document).ready?欣賞是否有人可以爲我提供一個簡單易用的示例來學習$(document).ready。

<script>$(document).ready(function() 
{ 
// function implementation internals 
}); 
</script> 

由於事先 喬治

回答

2

所有你需要的是jQuery庫。

http://www.jquery.com

您可以下載庫,包括它從自己的服務器,或者你可以使用的許多CDN的其提供的庫之一。例如:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
      // do something useful 
    }); 
</script> 
+0

謝謝,有點困惑。你能讓我知道我需要什麼代碼來引用嗎? – George2 2010-12-11 10:13:35

+1

@ George2從http://docs.jquery.com/Downloading_jQuery下載幷包含js文件 – 2010-12-11 10:16:06

+0

謝謝jAndy! – George2 2010-12-11 10:20:33

1

谷歌一直在自己的服務器,這是相當可靠的上a bunch of libraries副本。

只需將以下內容添加到您的<head>部分,並將您的代碼段放置在下面的某處。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 
+0

謝謝,問題回答! – George2 2010-12-11 10:20:03

+1

我不會建議在''部分添加腳本標籤。在''標籤末尾加上這些標籤可能是一個更好的主意。 – jAndy 2010-12-11 10:21:36

1

綜上所述,

將通過sje397在頁面的<head>部分,它提供了你需要的唯一庫... jQuery的提供的<script>標籤。

(或者:<script src="http://code.jquery.com/jquery-1.4.4.js" type="text/javascript"></script>

http://docs.jquery.com/Tutorials:How_jQuery_Works

,你應該是好去。

1
<html> 
<head> 
</head> 
<body> 

<div id="someElement">Click Me</div> 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 
<script type="text/javascript"> 
    (function($) { 
     $(document).ready(function() { 

      $('#someElement').bind('click', function(event) { 
       // event.preventDefault(); // you might want to do this if your event handler has a default action associated with it (e.g. a link that gets clicked with an href) 
       // do stuff on your event (change click to whatever you need) 
      }); 

     }); 
    })(jQuery); 
</script> 
</body> 
</html>