简体   繁体   English

当MasterPage位于根文件夹中时,在子文件夹中使用JQuery

[英]Using JQuery in a Subfolder When the MasterPage is in the Root Folder

I am trying to use the jquery library in ASP.NET in a subfolder called "samples" with a masterpage that is located in the root directory. 我试图在ASP.NET中的jquery库中使用名为“samples”的子文件夹,其中的母版页位于根目录中。 Presently the references to the jquery scripts are located in the head tag of the master page. 目前,对jquery脚本的引用位于母版页的head标记中。 If the page I am creating is also in the root directory, everything works fine. 如果我正在创建的页面也在根目录中,一切正常。 If I move the page to the "samples" subdirectory, the jquery breaks. 如果我将页面移动到“samples”子目录,则jquery会中断。

I can fix the problem by using something like the following in the head tag: 我可以通过在head标签中使用以下内容来解决问题:

<script src="<%=ResolveUrl("~/js/jquery.js")%>" type="text/javascript"></script>

...but then I lose the ability to use jquery intellisense, because I am no longer directly connected to the file in design time. ...但后来我失去了使用jquery intellisense的能力,因为我不再在设计时直接连接到文件。

So my quesiton is this: How can I use the jquery library on a .aspx page without losing connectivity to the intellisense when my page is in a subfolder and the master page is in the root? 所以我的问题是这样的:当我的页面在子文件夹中并且母版页在根目录中时,如何在.aspx页面上使用jquery库而不会失去与intellisense的连接?

simply use this: 只需使用:

<script src="/js/jquery.js" type="text/javascript"></script>

putting / before js do the trick. / js 之前做的伎俩。 I always keep my css and javascript files in separate folders and use this tweak to rightly include them. 我总是将我的css和javascript文件保存在单独的文件夹中,并使用此调整正确地包含它们。

for intellisense you can try this trick: 对于intellisense你可以尝试这个技巧:

<%if(true){%>
    <script src="/js/jquery.js" type="text/javascript"></script>
<%}%>

I'm not able to recall the source of this trick. 我无法回想起这个伎俩的来源。

You could use a script manager to include the JS files: 您可以使用脚本管理器来包含JS文件:

<asp:ScriptManager runat="server">
    <Scripts>
        <asp:ScriptReference Path="~/js/jquery.js" />
    </Scripts>
</asp:ScriptManager>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM