嗯,我明白我的标题有点令人困惑。下面我将通过示例清晰地表述。
<asp:ImageButton ID="imgButton" NavigateUrl="~/Presentation/Resources/Images/addFile.png" runat="server" />
在HTML中,上述控件将被呈现为:
<input type="image" name="imgButton" id="imgButton" src="../../Resources/Images/addFile.png" style="border-width:0px;">
我注意到,它会将src从“~”转换为“../../”。它会自动安排文件级别。
所以在JavaScript中,我想使用以下URL来设置控件:
~/Presentation/Resources/Images/PDF.png
不幸的是,在HTML中它将被呈现为:
<input type="image" name="imgButton" id="imgButton" src="~/Presentation/Resources/Images/addFile.png" style="border-width:0px;">
我的问题是,如果我想用“~”获取“../../”相对路径,我应该写什么? 我尝试过这个,但是我无法得到它。
<script type="javascript">
document.getElementById("<%= imgButton.ClientID %>").src =
"~/Presentation/Resources/Images/PDF.png";
</script>