单击Anchor 搜索不存在的重复嵌套层次结构中的页面

时间:2021-11-08 21:16:00

I have a list of menus as below, every menu link is an anchor. I am experiencing a very strange behaviour from anchor tag i.e., when I click on any menu it opens the requested page correctly. But on second click system duplicates the directory hierarchy and looks for the requested page where hierarchy does not exist. For example there is a page on path "Pages/Contact/test.aspx", 1st click opens the page. When user clicks 2nd time browser tries to open a page at path "Pages/Contact/Pages/Contact/test.aspx" resulting in exception "The resource cannot be found" error is thrown.

我有一个菜单列表如下所示,每个菜单链接都是一个锚点。我正在经历锚标记的一个非常奇怪的行为,即,当我点击任何菜单时,它正确打开所请求的页面。但是在第二次单击系统时,复制目录层次结构并查找不存在层次结构的请求页面。例如,路径“Pages / Contact / test.aspx”上有一个页面,第一次单击打开页面。当用户单击第二次浏览器尝试在路径“Pages / Contact / Pages / Contact / test.aspx”中打开页面时,会导致异常“无法找到资源”错误。

<div id="menu">
    <ul class="menu">
        <li><a href="" class="parent"><span>About Us</span></a>
            <div>
                <ul>
                    <li><a href="Pages/Contact/Phone.aspx" ><span>Phone</span></a></li>
                    <li><a href="Pages/Contact/Email.aspx" ><span>Email</span></a></li>

                </ul>
            </div>
        </li>
        </ul>
        </div>

2 个解决方案

#1


1  

The problem is because you are using relative paths to your pages.

问题是因为您正在使用页面的相对路径。

It works the first time because (I assume) you are in the root directory. So clicking the link takes you to 'Pages/Contact/Phone.aspx'. When in phone.aspx, if you click the link again, it looks for this page: Pages/Contact/Pages/Contact/Phone.aspx.

它是第一次工作,因为(我假设)你在根目录中。因此,单击该链接将转到“Pages / Contact / Phone.aspx”。在phone.aspx中,如果再次单击该链接,它将查找此页面:Pages / Contact / Pages / Contact / Phone.aspx。

You need to add a / to the beginning of your URL to make it relative to the root of the site:

您需要在URL的开头添加/以使其相对于站点的根目录:

<ul>
    <li><a href="/Pages/Contact/Phone.aspx" ><span>Phone</span></a></li>
    <li><a href="/Pages/Contact/Email.aspx" ><span>Email</span></a></li>
</ul>

Or alternatively, because you're using ASP.Net, you can use the ResolveUrl() function to ensure all your links are relative to the root of the solution:

或者,因为您正在使用ASP.Net,您可以使用ResolveUrl()函数来确保所有链接都相对于解决方案的根目录:

<ul>
    <li><a href="<%= ResolveUrl("~/Pages/Contact/Phone.aspx") %>"><span>Phone</span></a></li>
    <li><a href="<%= ResolveUrl("~/Pages/Contact/Email.aspx") %>"><span>Email</span></a></li>
</ul>

#2


0  

I believe href="" is your problem here.

我相信href =“”是你的问题。

Try href="#" or href="javascript:;"

尝试href =“#”或href =“javascript:;”

#1


1  

The problem is because you are using relative paths to your pages.

问题是因为您正在使用页面的相对路径。

It works the first time because (I assume) you are in the root directory. So clicking the link takes you to 'Pages/Contact/Phone.aspx'. When in phone.aspx, if you click the link again, it looks for this page: Pages/Contact/Pages/Contact/Phone.aspx.

它是第一次工作,因为(我假设)你在根目录中。因此,单击该链接将转到“Pages / Contact / Phone.aspx”。在phone.aspx中,如果再次单击该链接,它将查找此页面:Pages / Contact / Pages / Contact / Phone.aspx。

You need to add a / to the beginning of your URL to make it relative to the root of the site:

您需要在URL的开头添加/以使其相对于站点的根目录:

<ul>
    <li><a href="/Pages/Contact/Phone.aspx" ><span>Phone</span></a></li>
    <li><a href="/Pages/Contact/Email.aspx" ><span>Email</span></a></li>
</ul>

Or alternatively, because you're using ASP.Net, you can use the ResolveUrl() function to ensure all your links are relative to the root of the solution:

或者,因为您正在使用ASP.Net,您可以使用ResolveUrl()函数来确保所有链接都相对于解决方案的根目录:

<ul>
    <li><a href="<%= ResolveUrl("~/Pages/Contact/Phone.aspx") %>"><span>Phone</span></a></li>
    <li><a href="<%= ResolveUrl("~/Pages/Contact/Email.aspx") %>"><span>Email</span></a></li>
</ul>

#2


0  

I believe href="" is your problem here.

我相信href =“”是你的问题。

Try href="#" or href="javascript:;"

尝试href =“#”或href =“javascript:;”