为什么我仍然需要在我的视图页面中引用 jquery,当它被捆绑并加载到 _layout.cshtml 中时?
如果我不在我的视图页面中引用 jquery.js 文件,我的 jquery 将无法工作。
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*"));
bundles.Add(new ScriptBundle("~/Scripts/Bootstrap").Include(
"~/Scripts/jquery-1.*",
"~/Scripts/Bootstrap.js",
"~/Scripts/bootstrap-modalmanager.js",
"~/Scripts/bootstrap-modal.js"));
bundles.Add(new StyleBundle("~/Content/Bootstrap").Include(
"~/Content/Site.css",
"~/Content/Bootstrap.min.css",
"~/Content/Bootstrap-responsive.css",
"~/Content/bootstrap-modal.css"
));
}
在我看来,如果不引用 jquery,这将不起作用:
@{
ViewBag.Title = "Index";
}
<script>
$(document).ready(function () {
$("#btn1").click(function () {
alert("Text: " + $("#test").text());
});
$("#btn2").click(function () {
alert("HTML: " + $("#test").html());
});
});
</script>
<p id="test">This is some <b>bold</b> text in a paragraph.</p>
<button id="btn1">Show Text</button>
<button id="btn2">Show HTML</button>
这适用于添加参考:
@{
ViewBag.Title = "Index";
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
</script>
<script>
$(document).ready(function () {
$("#btn1").click(function () {
alert("Text: " + $("#test").text());
});
$("#btn2").click(function () {
alert("HTML: " + $("#test").html());
});
});
</script>
<p id="test">This is some <b>bold</b> text in a paragraph.</p>
<button id="btn1">Show Text</button>
<button id="btn2">Show HTML</button>