如何呈现与波浪相对路径到../../ jQuery中/ JavaScript的相对路径? [英] how to render the relative path with tilde into ../../ of relative path in jquery/javascript?
问题描述
好吧,我明白我的标题是有点混乱。我会用例子说明它明显低于
< ASP:ImageButton的ID =imgButtonNavigateUrl =〜/ presentation /资源/图像/ addFile.png=服务器/>
在HTML中,控制上述将呈现为
<输入类型=形象NAME =imgButtonID =imgButtonSRC =../../资源/图像/ addFile.png的风格=边框宽度:0像素;>
我注意到,它会在src转换〜到../../。它自动排列它将文件级别。
所以在JavaScript中,我想将它与这个网址控制:
〜/ presentation /资源/图像/ PDF.png
块引用>不幸的是,在HTML将被呈现为
<输入类型=形象NAME =imgButtonID =imgButtonSRC =〜/ presentation /资源/图像/ addFile.png的风格= 边框宽度:0像素;>
我的问题是,如果我想要得到的../../相对路径以〜?我应该写什么
我已经试过这一点,但我不能得到它。<脚本类型=JavaScript的>
的document.getElementById(&下;%= imgButton.ClientID%gt;中)。SRC =
〜/ presentation /资源/图像/ PDF.png
< / SCRIPT>
解决方案试试这个:<一href=\"http://weblogs.asp.net/joelvarty/archive/2009/07/17/resolveurl-in-javascript.aspx\">http://weblogs.asp.net/joelvarty/archive/2009/07/17/resolveurl-in-javascript.aspx
在该网站的主页,把这个:
块引用>&LT;脚本类型=文/ JavaScript的&GT;
变种的baseUrl =下;%= RESOLVEURL(〜/)%&gt;中;
&LT; / SCRIPT&GT;
然后,在你的JavaScript文件,把这个功能:
块引用>函数RESOLVEURL(URL){
如果(url.indexOf(〜/)== 0){
URL = +的baseUrl url.substring(2);
}
返回URL;
}
您可能已经把在母版页功能吧,但你
不会得到智能影音意义上为您的code的其余部分。轮到你了
可以用〜/右从JavaScript调用RESOLVEURL。
块引用>为什么需要这对客户方?使用servercontrols(
RUNAT =服务器
),你可以用波浪来解决服务器的URL。Well, i understand my title is a bit confusing. I will state it clearly below with the example.
<asp:ImageButton ID="imgButton" NavigateUrl="~/Presentation/Resources/Images/addFile.png" runat="server" />
In html, the control above will be rendered as
<input type="image" name="imgButton" id="imgButton" src="../../Resources/Images/addFile.png" style="border-width:0px;">
I notice that,it will convert the src from "~" to "../../" .It auto arrange it will the file level.
so in javascript, i want to set it the control with this url :
~/Presentation/Resources/Images/PDF.png
unfortunately, in html it will be rendered as
<input type="image" name="imgButton" id="imgButton" src="~/Presentation/Resources/Images/addFile.png" style="border-width:0px;">
My question is, What should i write if i wanna get the "../../" relative path with "~" ? I have tried this,but i cant get it.
<script type="javascript"> document.getElementById("<%= imgButton.ClientID %>").src = "~/Presentation/Resources/Images/PDF.png"; </script>
解决方案Try this: http://weblogs.asp.net/joelvarty/archive/2009/07/17/resolveurl-in-javascript.aspx
In the master page for the site, put this:
<script type="text/javascript"> var baseUrl = "<%= ResolveUrl("~/") %>"; </script>
Then, in your javascript file, put this function:
function ResolveUrl(url) { if (url.indexOf("~/") == 0) { url = baseUrl + url.substring(2); } return url; }
You could have put the function right in the master page, but then you wouldn’t get intelli-sense on it for the rest of your code. Now you can call ResolveUrl with ~/ right from javascript.
Why do you need this on clientside? Use servercontrols(
runat=server
) and you can use tilde to resolve URL on server.这篇关于如何呈现与波浪相对路径到../../ jQuery中/ JavaScript的相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!