我第一次在我的网站上尝试jQuery而且它不起作用。我现在已经尝试了大约10个小时的数百件事,今天我有更多的白发=)。我找到的最简单的片段就是这个:
<script type="text/javascript">
$(document).ready(function () {
$("#msgid1").html("This is Hello World by JQuery 1<BR>");
});
$(function () {
$("#msgid2").html("This is Hello World by JQuery 2<BR>");
});
window.onload = function() {
document.getElementById('msgid3').innerHTML = "This is Hello World by JavaScript";
};
</script>
This is Hello World by HTML
<div id="msgid1">
</div>
<div id="msgid2">
</div>
<div id="msgid3">
</div>
我把它放在我的<body>
中。结果是:
This is Hello World by HTML
This is Hello World by JavaScript
jQuery调用对我不起作用。也许它与我的<head>
有关?
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" media="screen"/>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script>
您怎么看?
答案 0 :(得分:2)
我猜你要么你的脚本指向这个脚本块下面的jQuery,要么完全没有。你需要&#34;加载&#34; jQuery在你尝试使用它之前:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#msgid1").html("This is Hello World by JQuery 1<BR>");
});
$(function () {
$("#msgid2").html("This is Hello World by JQuery 2<BR>");
});
window.onload = function() {
document.getElementById('msgid3').innerHTML = "This is Hello World by JavaScript";
};
</script>
我添加的脚本标记指向Google CDN,但您也可以在本地下载并在本地引用:
<script type="text/javascript" src="scripts/jquery.min.js"></script>
答案 1 :(得分:0)
实际上,我刚刚找到了解决方案。
似乎存在这些线彼此不相邻的问题
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
通过简单地改变行的顺序,两个jQuery输出行突然出现了!
谢谢大家的帮助!