2014-12-07 57 views
1

有没有人有任何想法为什么下面的代码不工作?不加载jQuery版本

<!DOCTYPE html> 
<head> 
    <title> The wonderfulness of JS and jQuery </title> 
    <meta charset="utf-8"> 
    <script src="http://code.jquery.com/jquery-latest.min.js"> 
    <script type="text/javascript"> 
     function writeIt() { 
      document.write("jQuery version " + $().jquery + " loaded."); 
     } 
    </script> 
</head> 
<body onload="writeIt()"> 
Hello 
</body> 
</html> 

它没有显示jQuery版本。当我试图学习一些jQuery和JavaScript,并想知道为什么它不适合我时,我只是碰到了那段代码。

谢谢!

回答

4

这只是因为你没有结束script标签...

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

对于script标签这是绝对必需的。它需要完全合格,它不以/>结束。

+0

感谢。 jQuery版本正在显示,但是我在中放入的任何内容都不是。我如何让它们都可以工作? – Niralana 2014-12-07 14:54:34

+2

不要使用'document.write',这会覆盖任何内容。改用'innerHTML'或'$ .append'! – metadings 2014-12-07 14:55:22

+0

好的,谢谢你的帮助! – Niralana 2014-12-07 20:43:50

0

这个答案是基于您的评论部分,

<!DOCTYPE html> 
<head> 
<title> The wonderfulness of JS and jQuery </title> 
<meta charset="utf-8"> 
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script type="text/javascript"> 
    function writeIt() { 
     alert("jQuery version " + $().jquery + " loaded."); 
     /*document.getElementById("maincontent").innerHTML='Body Content';*/ 
     $('#maincontent').append('appended content'); 
     /*document.write("jQuery version " + $().jquery + " loaded.");*/ 

     /*if (typeof jQuery != 'undefined') { 
      alert(jQuery.fn.jquery); 
     } 
     }*/ 
    } 

</script> 
</head> 
<body id="maincontent" onload="writeIt()"> 
initial content 
</body> 
</html> 
+0

太好了,非常感谢! – Niralana 2014-12-07 20:45:22

+0

欢迎您:) – 2014-12-13 03:24:47