我使用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