简体   繁体   English

为什么我必须包含JS文件才能在每个视图中使用jquery?

[英]Why do i have to include JS files in order to work with jquery in every single view?

As the question says all about it but to give you a little more information i included all the .js files in my Asp.NET Mvc application like below: 正如问题所说的那样,但是为了给你提供更多信息,我在我的Asp.NET Mvc应用程序中包含了所有.js文件,如下所示:

<script src="~/Scripts/jquery-1.10.2.js"></script>

Even that I have to include .js files in every single page in order to be able to work with jQuery else it doesn't work. 即使我必须在每个页面中包含.js文件,以便能够使用jQuery,否则它不起作用。 I tried to include these script files in BundleConfig surprisingly the scripts where not working at all even on the layout page. 我试图将这些脚本文件包含在BundleConfig令人惊讶的是脚本甚至在布局页面上都没有工作。 What is the trick?! 有什么诀窍?!

In general you have got an App_Start/BundleConfig.cs in your project 通常,您的项目中有一个App_Start / BundleConfig.cs

public class BundleConfig
{
    public static readonly string SiteJquery1Bundle = "~/bundles/jquery1";
    public static readonly string SiteJquery2Bundle = "~/bundles/jquery2";

    public static void RegisterBundles(BundleCollection bundles)
    {
        // We will choose bundle to render depending on IE version in view
        bundles.Add(new Bundle(SiteJquery1Bundle)
               .Include("~/Scripts/jquery-1.10.2.js"));
        bundles.Add(new Bundle(SiteJquery2Bundle)
               .Include("~/Scripts/jquery-2.1.3.min.js"));

    }

In your Shared/_Layout.cshtml view add: 在您的Shared / _Layout.cshtml视图中添加:

<html>
<head>
<title>...</title>
<!--[if lt IE 9]>
@Scripts.Render(BundleConfig.SiteJquery1Bundle)
<![endif]-->
<!--[if gte IE 9]><!-->
@Scripts.Render(BundleConfig.SiteJquery2Bundle)
<!--<![endif]-->
...
</head>

So, default ASP.NET MVC projects adds _ViewStart.cshtml which defines single layout in every view. 因此,默认的ASP.NET MVC项目添加了_ViewStart.cshtml,它定义了每个视图中的单个布局。 And check bundle registration in Global.asax.cs 并检查Global.asax.cs中的包注册

protected void Application_Start()
{
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    // ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否必须在每个网页中都包含我所有的头部内容? - Do I have to include all of my head content in every single web page? 为什么必须重新加载页面才能使JS事件正常工作? - Why do I have to reload the page for my JS event to work? 为什么我必须在grunt-requirejs中再次使用&#39;include&#39;来优化成单个js文件? - Why I have to use 'include' again in grunt-requirejs to optimize into a single js file? 如何在include.js文件中包含jQuery脚本? - how do I include a jQuery script into an include.js file? 在Visual Studio中使用打字稿时,为什么需要在default.htm中包含多个.js文件? - Why do I need to include multiple .js files in default.htm when working with typescript in visual studio? 为什么我需要jquery.min.js而不是jquery.js才能使脚本起作用? - Why do I need jquery.min.js vs just jquery.js for script to work? (Webpack) 使用 url-loader 或 file-loader,我真的必须在我的 .js 中为我想要包含的每个静态图像包含一个 require() 吗? - (Webpack) Using the url-loader or file-loader, do I really have to include a require() in my .js for every static image I want to include? 当我通过php include将datepicker js&css文件包括在内时不起作用,但是如果我将所有代码都放在单个php页面中就可以使用 - When i include datepicker js & css files through php include doesn't work, but works on if i put all the code in single php page 如何使用 JS 或 jQuery 选择每个元素? - How can I select every single element with JS or jQuery? 为什么在Backbone / Require.js项目中的每个JS文件中都包含jQuery和下划线 - Why include jQuery and underscore in every JS file in Backbone/Require.js project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM