2014-09-27 44 views
-6

我剛接觸jQuery,似乎無法讓它在本地計算機上工作。我將生產jQuery 2.1.1的代碼保存爲jquery.min.js。但是當我嘗試在項目中使用jQuery時,它不起作用。如果我點擊我保存的jquery.min.js文件,它會放棄「默認視圖」爲空或不是對象的Microsoft腳本錯誤。我一直在使用這段代碼來查看是否安裝了jQuery。有任何想法嗎?讓jQuery工作的問題

<!doctype html> 
<html> 
    <head> 
    <title>Learning jQuery</title> 

    <meta charset="utf-8" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <script src="jquery.min.js"></script> 
    </head> 

    <body> 
    <script> 
     if (typeof jQuery != "undefined") { 
     alert('jQuery is installed!'); 
    </script> 
    </body> 
</html> 
+3

究竟什麼是你遇到的問題?當你加載你的頁面時,你是否得到警報? – Pointy 2014-09-27 19:15:56

+5

而且,jquery.min.js與HTML頁面位於同一目錄中嗎? – 2014-09-27 19:16:44

+0

我沒有得到警報,當我加載頁面,只是jQuery不起作用。 – Nkelly 2014-09-27 19:19:47

回答

0

您錯過了IF語句的大括號。

<!doctype html> 
<html> 
    <head> 
    <title>Learning jQuery</title> 

    <meta charset="utf-8" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <script src="jquery.min.js"></script> 
    </head> 

    <body> 
    <script> 
     if (typeof jQuery != "undefined") { 
     alert('jQuery is installed!'); 
     }         // <---- Right here! 
    </script> 
    </body> 
</html> 
+1

如果你認爲是問題所在,那麼投票結束這個問題就會因爲輸入錯誤而離題。 – j08691 2014-09-27 19:23:24

+0

這顯然是問題...我測試過了。然而,這可能是一個語法錯誤,而不是一個錯字,因此編碼錯誤,因此公平的遊戲。也許比我更有名譽的人應該做出關閉的決定。 – 2014-09-27 19:25:18

+0

他可以刪除開放的支撐! – XDnl 2014-09-27 19:26:33

0

嘗試使用jQuery的CDN檢查問題[來源:http://jquery.com/download/]

<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 

其他辦法,包括它是這樣的:http://www.w3schools.com/jquery/jquery_install.asp

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

也有被關閉括號丟失代碼

<!doctype html> 
<html> 
    <head> 
    <title>Learning jQuery</title> 

    <meta charset="utf-8" /> 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
    <script src="jquery.min.js"></script> 
    </head> 

    <body> 
    <script> 
     if (typeof jQuery != "undefined") { 
     alert('jQuery is installed!'); 
     }//** <--------here is the missing closing braces ** 

    </script> 
    </body> 
</html> 
+1

沒有協議的'src'地址被視爲本地資源,您必須指定協議,例如'http://' – 2014-09-27 19:26:05

+0

hitesh的答案中的剪輯似乎直接從Google複製,並且* does *工作;我剛剛測試過它。 – 2014-09-27 19:27:25

+0

你不需要指定一個協議。瀏覽器會爲你做到這一點。 – putvande 2014-09-27 19:27:55

0

我已經給你的代碼做了一些修改,請檢查一下

1.關閉大括號。

2.更改最小文件。

我認爲主要的錯誤是語法,因爲你沒有關閉if條件的大括號。

<!doctype html> 
 
<html> 
 
    <head> 
 
    <title>Learning jQuery</title> 
 

 
    <meta charset="utf-8" /> 
 
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 
 
    <meta name="viewport" content="width=device-width, initial-scale=1" /> 
 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
 
    
 
    </head> 
 

 
    <body> 
 
    <script> 
 
     if (typeof jQuery != "undefined") { 
 
     alert('jQuery is installed!'); 
 
     } 
 
    </script> 
 
    </body> 
 
</html>