2014-09-27 43 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>