如何在部分视图ASP.NET MVC 4中使用jQuery [英] How to use jquery in partial view asp.net mvc 4

查看:63
本文介绍了如何在部分视图ASP.NET MVC 4中使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究MVC-3 Web应用程序.现在我将其更改为mvc4,并将jQuery文件放在_Layout页面的末尾

I was working on MVC-3 web application. Now i changed it to mvc4 and put the jquery files in end of _Layout page

<html>
<head>
</head>
<body>
    @Html.Partial("_Menu")
    @RenderBody()
    @System.Web.Optimization.Scripts.Render("~/jquery")
</body>
</html>

我已经在Partial View"_Menu"中使用了一些jquery,在Mvc 3中,这很好用,因为我将jquery文件放在了head标签中,但是现在当我调用此局部视图时,我面临着问题

I have used some jquery in Partial View "_Menu", in Mvc 3 this is working fine because i put jquery files in head tag but now i am facing issue when i call this partial view

未捕获的ReferenceError:未定义$

Uncaught ReferenceError: $ is not defined

我认为此问题是由于jquery文件正在页面末尾加载而引起的.我想到的解决方案是将jquery文件加载到head标签上,但我不想这样做.

I think this problem is due to jquery files are loading at the end of the page. Solution that comes in my mind is to load jquery files on head tag but i don't want to do this.

建议我其他解决方案.如何在局部视图中使用jquery.

Suggest me any other solution. How can i use jquery in partial view.

谢谢

推荐答案

如果将jQuery代码放在外部脚本文件中,则可以利用script元素的defer属性,如下所示:

If you put the jQuery code in an external script file then you can take advantage of the defer attribute for the script element as follows:

<script type="text/javascript" src="<path to your .js file>" defer></script>

因此,您的部分视图将包含此script标记,并且'defer'阻止浏览器运行脚本,直到页面加载完毕为止,这意味着jQuery库在执行时将存在.

So your partial view would include this script tag and 'defer' stops the browser from running the script until after the page has loaded, which means that the jQuery libraries will exist when it executes.

这篇关于如何在部分视图ASP.NET MVC 4中使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆