如何在部分视图ASP.NET MVC 4中使用jQuery

编程入门 行业动态 更新时间:2024-10-24 16:28:17
本文介绍了如何在部分视图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

本文发布于:2023-11-15 10:34:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1593655.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   如何在   ASP   jQuery   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!