ASP.NET MVC jQuery文件就绪函数未调用

时间:2016-11-16 13:04:38

标签: html asp.net-mvc asp.net-ajax

我有一个包含此脚本的Razor视图:

<script type="text/javascript">
    jQuery(function()
    {
        alert("hello world");
    })
</script>

脚本在页面加载时打开警报对话框。

BundleCfg的部分:

    bundles.Add(New ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js",
                "~/Scripts/jquery.unobtrusive-ajax.js",
                "~/Scripts/jquery.validate.unobtrusive.js"))

    bundles.Add(New ScriptBundle("~/bundles/jqueryval").Include(
                "~/Scripts/jquery.validate*"))

在布局体的末尾:

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required:=False)

3 个答案:

答案 0 :(得分:1)

试试这个

$(document).ready(function() {
    alert("hello world");
});

这里有详细信息: https://learn.jquery.com/using-jquery-core/document-ready/

根据上面的链接,我意识到你的代码:

<script type="text/javascript">
    jQuery(function()
    {
        alert("hello world");
    });
</script>    

与此相同

<script type="text/javascript">
  $(document).ready(function() {
      alert("hello world");
  });
</script>

所以问题只是关于渲染包的位置。

答案 1 :(得分:0)

用此更改您的代码。

$(document).ready(function () {
        alert("Hello World");
 });

答案 2 :(得分:0)

问题是在调用符号$ (jQuery)之后加载了jQuery库;这就是为什么不起作用。

您必须输入以下行:

@Scripts.Render("~/bundles/jquery")

在渲染主体之前。

相关问题