为什么以下jquery在jsfiddle中工作,但在任何浏览器上都没有?

时间:2012-08-14 13:37:00

标签: jquery

我尝试在FF和chrome中打开以下代码,没有任何反应。但是当我在jsfiddle中加载确切的代码时,它起作用了。

jsfiddle link

HTML:

<!doctype html>

<html>
<head>
<link rel="stylesheet" type="text/css" media="screen" href="fgerg.css" />
<script type="text/javascript" src="jquery-1.7.2.js"></script>


<script type="text/javascript">
                $(document).ready(function() {



                    $('#showButton').click(function() {
                        $('#x2').css('visibility','visible');


                    });     
                });​

            </script>   
</head>

<body>
 <input type="button" id="showButton" value="show" />
    <p id="x"> gergreg</p>
    <p id="x2"> sadeijnfciu </p>        
</body>
    </html>

CSS:

#x {
display:none;
 }

 #x2 {
visibility: hidden;
}

对于任何感兴趣的人,最后});之后的矩形导致整个混乱。谁知道那是什么? Wierd special character

5 个答案:

答案 0 :(得分:2)

javascript代码看起来有效。

您确定jquery-1.7.2.js文件与index.html位于同一目录中吗?

+my webpage
-----jquery-1.7.2.js
-----index.html
-----.......

你的目录是这样的吗?

我建议你使用google托管的jQuery libary,如下所示:

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

答案 1 :(得分:2)

错误:未捕获的SyntaxError:意外的令牌ILLEGAL 通常意味着您的页面/ JavaScript源中某处有一些奇怪的编码字符。最好的办法是用showing special characters打开文件编辑器,或者创建一个新文件并复制代码部分。

答案 2 :(得分:1)

很可能您的jquery链接已损坏,或者您使用的是损坏的版本。尝试从CDN中包含jQuery:

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

而不是

<script type="text/javascript" src="jquery-1.7.2.js"></script>

答案 3 :(得分:0)

除非您有使用visibility的具体原因,否则我更愿意使用.show()http://jsfiddle.net/Nuh9w/3/

答案 4 :(得分:0)

当您在FF或Chrome中运行代码时,您是否在与HTML文件相同的相对路径中引用了jQuery文件?当您在jsfiddle中执行此操作时,jsfiddle站点将加载jQuery库。