2013-04-17 77 views
1

我無法得到這個簡單的腳本運行,並一直盯着它一段時間,並沒有看到任何語法錯誤。只需要警報在點擊時顯示。jQuery按鈕點擊不會運行

<html> 
<head> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

<script type='text/javascript'> 

$(document).ready(function() { 
    $("#authenticate_button").click(function() { 
     alert("click"); 
    }); 
}); 

</script> 

</head> 
<body> 
    <input id="code" type="text"> 
    <button id='authenticate_button'>Authenticate</button> 
</body> 
</html> 
+4

你打開http中的文件://?如果你打開它在文件中://它不能工作。 –

+0

瀏覽器中的控制檯說什麼? – orolo

+1

這不是在本地文件中運行嗎? –

回答

6

您正在用您的網絡瀏覽器直接打開文件。 //是您當前協議的縮寫(即file://),所以jQuery不會從Google的CDN加載。

您需要在此行之前//加入http:明確指定的協議:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

我也想補充一個文檔類型:

<!DOCTYPE html> 
+0

我想是這樣的,http://jsfiddle.net/GrVke/似乎在那裏工作... – Bene

+0

啊geez我永遠不會注意到,謝謝。 – ThingWings

0

你也可以嘗試這個:

使用由MediaTemple提供的jQuery的CDN

<script src="http://code.jquery.com/jquery-1.9.1.js"></script> 

使用微軟的CDN

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js"></script> 

來替代jQuery的js文件。

我建議去這樣的:

<script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.min.js"></script> 
<script> 
    // Fallback to loading jQuery from a local path if the CDN is unavailable 
    (window.jQuery || document.write('<script src="/scripts/jquery-1.9.0.min.js"><\/script>')); 
</script>