我使用JavaScript动态创建包含链接和URL的子菜单项。但是,当我导航到链接时,URL不会将~
解析为根目录,而只是将该字符串附加到当前URL。在动态创建的链接中使用波浪号(〜)
HTML:
<!DOCTYPE html>
<html>
<body>
<div id="left-menu" class="sidr-hide">
<ul>
<li><a href="~/">Home</a></li>
<li><a href="~/ListItem1">ListItem1</a></li>
<li><a href="~/ListItem2">ListItem1</a>
<ul id="ListItemDropdown">
Following items will be created Dynamically
@* <li><a href="~/ListItem2/?param=SubItem1">SubItem1</a></li>
<li><a href="~/ListItem2/?param=SubItem2">SubItem2</a></li>*@
</ul>
</li>
Added here for link testing:
<li><a href="~/ListItem2/?param=SubItem1">SubItem1</a></li>
<li><a href="~/ListItem2/?param=SubItem2">SubItem2</a></li>
</ul>
</div>
<script src="~/Scripts/Layout.js"></script>
</body>
</html>
是直接在上面,做工精细的HTML标签写的所有链接。
的JavaScript:
var ExchangeLeftDropdown = document.getElementById("ListItemDropdown");
$(document).ready(function() {
var test = "Subitem1}SubItem2}"; //This string will be filled by a controller later on
var test2 = test.split("}");
for (i = 0; i < test2.length; i++) {
//Create List Item
var newListItem = document.createElement("li");
//Create Link Item
var itemLink = document.createElement("a");
//Add text to link Item
itemLink.textContent = test2[i];
//Add link URL to link Item
itemLink.setAttribute("href", "~/ListItem2/?dom=" + test2[i]);//Desired URL,
//but this tilde doesn't equate to home directory, so instead I get this
//in the URL http://localhost:1461/'DirectoryCurrentlyViewing'/~/ListItem2/?dom=SubItem2.
//it does lead to the right place if you navigate from the home directory
// itemLink.setAttribute("href", "<%=ResolveUrl(~/Exchange/?dom=" + test2[i] + ")%>");
// This attempt resulted in me getting a 404 error vs a Server Error in '/' Application
//The URl is again a directly pasted string: http://localhost:1461/ListItem2/<%=ResolveUrl(~/ListItem2/?dom=SubItem2)%%>
itemLink.setAttribute("runat", "server"); //The attempt to parse the URL server side didn't have any effect
newListItem.appendChild(itemLink);
ExchangeLeftDropdown.appendChild(newListItem);
}
});
是否有链路创建我可以改变/添加或文本插入链接创建,将允许网站解析~
为根目录中的任何财产?
看起来你很迷惑[ASP.NET的'〜'在路径中](https://msdn.microsoft.com/en-us/library/ms178116.aspx)的东西,可以在HTML中使用一般。如果你想要根相关的URL,只需使用相对URL并离开'〜'。 – zzzzBov