在脚本标签src属性中使用波浪号 [英] Using tilde in script tag src attribute
问题描述
在我的asp.net网站中,使用 MasterPage
和 Routing
在 link
标记的> href 属性在MasterPage的 head
部分中。像这样:
In my asp.net website using MasterPage
and Routing
I use a tilde in the href
attribute of the link
tag for the stylesheet in the head
section of the MasterPage. Like this:
<link href="~/Styles/Main.css" rel="stylesheet" type="text/css" />
这就像一个护身符。由于网站使用路由,因此该网址将包含越来越多的 /
,但是样式表的href仍然有效,因为波浪号指向Web应用程序的根目录并使用了样式。
Which works like a charm. Since the website uses routing the url will contain more and more /
, yet the stylesheet's href remains valid because the tilde points to the root of the web application and the styles are used.
我尝试对脚本$的
src
属性使用相同的技术c $ c>标记,但这似乎无法产生预期的结果。我尝试过:
I tried using the same technique for the src
attribute of the script
tags, but this doesn't seem to produce the expected result. I tried:
<script src="~/Scripts/jquery-1.8.2.min.js" type="text/javascript" ></script>
但这只是将波浪号字符输出到页面的HTML中,而不是将其替换为Web应用程序,就像处理 href
属性一样。我的经验是,asp.net替换 href
属性中的波浪号,而不替换 src
属性中的波浪号。
But this just outputs the tilde character to the HTML of the page instead of replacing it with the root of the web application as it does for the href
attribute. My experience is that asp.net replaces tilde in href
attributes but not in src
attributes.
如何在 src
脚本标记属性中使用波浪号?
How can I make the tilde work in the src
atrribute of script tags?
推荐答案
我不确定是否有办法让它在没有一点帮助的情况下正常工作。这应该可行,但是不如链接
好:
I'm not sure there is a way to get it to work correctly without a bit of assistance. This should work, not as nice as the link
though:
<script src="<%=ResolveUrl("~/Scripts/jquery-1.8.2.min.js")%>"></script>
这篇关于在脚本标签src属性中使用波浪号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!