I'm trying to run a jquery code which was put in my _Layout.cshtml as below:
我正在尝试运行一个jquery代码,它放在我的_Layout.cshtml中,如下所示:
...............
<script type='text/javascript'>
$(document).ready(function () {
alert('Test');
});
</script>
@Scripts.Render("~/bundles/jquery")
@RenderSection("Scripts", required: false)
</body>
</html>
The above code wasn't fired and when I inspected with Chrome Dev, it showed $ is not defined ( I can see all my jquery, jquery ui files are loaded )
上面的代码没有被解雇,当我用Chrome Dev检查时,它显示$未定义(我可以看到我的所有jquery,jquery ui文件都已加载)
It worked if I put this code in an external js file
如果我将此代码放在外部js文件中,它就可以工作
I don't mind putting all my jquery code in external files, but really wanna clarify where I am doing wrong.
我不介意将我的所有jquery代码放在外部文件中,但我真的想澄清我做错了什么。
3 个解决方案
#1
23
You need to introduce jquery before your script.
您需要在脚本之前引入jquery。
@Scripts.Render("~/bundles/jquery")
<script type='text/javascript'>
$(document).ready(function () {
alert('Test');
});
</script>
@RenderSection("Scripts", required: false)
</body>
</html>
#2
7
Andreas, i have the same issue.. for default the "_layout.cshtml" has "@Scripts.Render("~/bundles/jquery")" at the end of the document, but it doesn't work.. i cut it and paste it in the head and now it is working.
安德烈亚斯,我有同样的问题..默认情况下,“_layout.cshtml”在文档的末尾有“@ Scripts.Render(”〜/ bundles / jquery“)”,但它不起作用..我切它并将其粘贴在头部,现在它正在工作。
#3
7
In order for jQuery to work, your script should come after <script src="/Scripts/jquery-2.0.3.js"></script>
为了使jQuery工作,你的脚本应该在
In the _Layout.cshtml, just before the </body>
tag you can find something like this
在_Layout.cshtml中,就在 标记之前,您可以找到类似这样的内容
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
First you have the jQuery library. Then you have any scripts. That's what this means. So in your views do something like this.
首先你有jQuery库。然后你有任何脚本。这就是这意味着什么。所以在你的观点中做这样的事情。
@section scripts{
//your script
}
This will make sure that all scripts comes under jQuery reference.
这将确保所有脚本都在jQuery引用下。
#1
23
You need to introduce jquery before your script.
您需要在脚本之前引入jquery。
@Scripts.Render("~/bundles/jquery")
<script type='text/javascript'>
$(document).ready(function () {
alert('Test');
});
</script>
@RenderSection("Scripts", required: false)
</body>
</html>
#2
7
Andreas, i have the same issue.. for default the "_layout.cshtml" has "@Scripts.Render("~/bundles/jquery")" at the end of the document, but it doesn't work.. i cut it and paste it in the head and now it is working.
安德烈亚斯,我有同样的问题..默认情况下,“_layout.cshtml”在文档的末尾有“@ Scripts.Render(”〜/ bundles / jquery“)”,但它不起作用..我切它并将其粘贴在头部,现在它正在工作。
#3
7
In order for jQuery to work, your script should come after <script src="/Scripts/jquery-2.0.3.js"></script>
为了使jQuery工作,你的脚本应该在
In the _Layout.cshtml, just before the </body>
tag you can find something like this
在_Layout.cshtml中,就在 标记之前,您可以找到类似这样的内容
@Scripts.Render("~/bundles/jquery")
@RenderSection("scripts", required: false)
First you have the jQuery library. Then you have any scripts. That's what this means. So in your views do something like this.
首先你有jQuery库。然后你有任何脚本。这就是这意味着什么。所以在你的观点中做这样的事情。
@section scripts{
//your script
}
This will make sure that all scripts comes under jQuery reference.
这将确保所有脚本都在jQuery引用下。