有谁知道为什么下面的代码不起作用?
<!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时遇到了那段代码,并想知道为什么它不适合我。
谢谢!
答案 0 :(得分:4)
这只是因为你没有结束script
标签......
<script src="http://code.jquery.com/jquery-latest.min.js"> </script>
对于script
标记,它是绝对必需的。它需要完全合格,并不以/>
结束。
答案 1 :(得分: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>