我第一次在我的网站上尝试 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>
你怎么看?
最佳答案
我猜你要么让你的脚本指向这个脚本块下面的 jQuery,要么完全不存在。在尝试使用它之前,您需要“加载”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>
我加的script标签指向的是谷歌CDN,但也可以本地下载,本地引用:
<script type="text/javascript" src="scripts/jquery.min.js"></script>
关于jquery - 调用 jQuery 不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8792073/