我正试图在母版页中添加对jQuery的脚本引用,以便它可以用于任何页面.现在看起来是这样的

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

The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:

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

这显然不太理想,因为它只适用于距离根文件夹两级深的页面.如果我try 执行以下操作,IIS将引发有关意外字符的错误.

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

有什么 idea 吗?

EDIT:.我还忘了提到 playbook 必须放在头标签上

The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.

The third answer throws a "Illegal characters in path." exception from the compiler

EDIT 2:当我把这一行添加到我的头标签时,我从IIS中得到了这个错误.

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

SOLVED:我从下面的答案中获取经过编辑的答案,并将其放入asp:ContentPlaceHolder元素中

推荐答案

你可以用ScriptManager:

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

EDIT: If you absolutely need this in your <head> section, you could do something like:

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

EDIT 2:根据 comments ,如果你观察到

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

您可能需要更改以上内容才能使用数据绑定语法:

<head>
    <script type="text/javascript" 
        src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>

Jquery相关问答推荐

try 使用jQuery AJAX将参数传递给http Post方法时出现未知错误

ASP.NET Core 8 MVC:从jQuery发布控制器中的所有值为空

jQuery:如何动态检测窗口宽度?

$.post 和 $.ajax 之间的区别?

使用淡入淡出和追加

如何用 javascript/jquery 替换 url 参数?

为什么我们在 jQuery 中使用({ })?

对混合的字母/数字数组进行排序

调用 jquery ajax - .fail vs. :error

如何从 jQuery 中的父级中 Select 所有子级(任何级别)?

moment.js isValid 函数无法正常工作

jQuery ajax 在 asp.net mvc 中上传文件

jQuery Force 为 iframe 设置 src 属性

如何检测 window.print() 完成

Rails 5:如何将 $(document).ready() 与 turbo-links 一起使用

Jquery:如何判断元素是否具有某些 css 类/样式

消除移动 Safari 中点击事件的 300 毫秒延迟

jQuery Uncaught TypeError: 对象 [object Window] 的属性$不是函数

当 iframe 在 jQuery 中完成加载时,如何触发事件?

使用 Ajax 下载并打开 PDF 文件