Referenceerror: $ is not defined

Asked

Viewed 94 times

-2

Hello, I created an application a short time ago and got jquery via Nuget, but it keeps giving error when initializing jquery. I have the following function in a js file.

$("#myNav").bind("mousewheel", function (ev, delta) {
    var scrollTop = $(this).scrollTop();
    $(this).scrollTop(scrollTop - Math.round(delta));
}); 

And my Layout has the following HTML

<!DOCTYPE html>
<html>
<head>
    <script src="~/Scripts/View/Layout.js" type="text/javascript"></script>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DK Design - @ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</head>
<body>
    <div>
        <div class="navbar navbar-light bg-light">
            <table style="width:100%">
                <tbody>
                    <tr>
                        <td align="right" valign="top" width="1%">
                            <button type="button" class="navbar-toggler" onclick="ControlNav()">
                                <span class="navbar-toggler-icon"></span>
                            </button>
                        </td>

                        <td align="center" width="90%">
                            <h1 class="logoFont">DK DESIGN</h1>
                        </td>

                        <td align="left" width="9%">
                            <span>Login </span><span> | </span><span> Registro</span>
                        </td>
                    </tr>
                </tbody>
            </table>

            <div id="myNav" class="overlay">
                <!-- Overlay content -->
                <div class="overlay-content">
                    @Html.ActionLink("Home", "Index", new { controller = "Home" }, new { @class = "font-custom-color" })
                    @Html.ActionLink("Contet Test", "Index", new { controller = "ContentTest" }, new { @class = "font-custom-color" })
                    <a href="#">Clients</a>
                    <a href="#">Contact</a>
                </div>

            </div>
        </div>
    </div>
    <div>
        @RenderBody()
    </div>

    <hr />
</body>
</html>

And the Bundle is like this:

using System.Web;
using System.Web.Optimization;

namespace DKDSGN
{
    public class BundleConfig
    {
        // Para obter mais informações sobre o agrupamento, visite https://go.microsoft.com/fwlink/?LinkId=301862
        public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js"));

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

            // Use a versão em desenvolvimento do Modernizr para desenvolver e aprender. Em seguida, quando estiver
            // pronto para a produção, utilize a ferramenta de build em https://modernizr.com para escolher somente os testes que precisa.
            bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                        "~/Scripts/modernizr-*"));

            bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
                      "~/Scripts/bootstrap.js"));

            bundles.Add(new StyleBundle("~/Content/css").Include(
                      "~/Content/bootstrap.css",
                      "~/Content/site.css"));
        }
    }
}

I tried to initialize jquery before the function, but it won’t. I tried to reference the files by src tbm but did not roll, could anyone help me ?

  • You can see everything that is happening on the page (including the source code) using Dev Tools (F12).

  • In the browser ? It shows this error in the console .. And indicates that the error is at the beginning of the function, but even a simple of Document.read it gives that same error.

  • Well, what I know is that the error quoted is a result of not loading jQuery.

  • Yes I noticed that, this is exactly what I’m asking, I tried to initialize by Bundle, ja tnetei initialize by src , he’s not referencing in the project at all

  • So I think the problem is in Asp.net. Then I don’t understand. Good luck there! :)

1 answer

0

Put that at the end of the View in that order:

--omitido
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
    <script src="~/Scripts/View/Layout.js" type="text/javascript"></script>
</body>
</html>

Check in Chrome’s Developer tools to see if the files have been loaded:

inserir a descrição da imagem aqui

If possible send a print of your Solution directories.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.