使用包含文件的锚点?

问题描述:

I try to use the anchor tag with the include I have. On my Index.php I got a little "nav bar" with some anchor tags. I also included the file "text.php" in my code. This file is in the Folder "texte". The anchor tags should bring the user to the position in text.php but it only load 404 - page not found.

How I include the file:

<?php include 'texte/text.php'; ?>

I declare my "start-word" like this:

<a href=“texte/text.php#InfoAnker“>Informationen</a>

And my anchor like this:

<h2 id="InfoAnker"> Informationen </h2>

I also tried <a href=“#InfoAnker“>Informationen</a> because of the include but it came out the same result.

Any ideas?

我尝试将锚标记与我拥有的包含一起使用。 在我的Index.php上,我得到了一个带有一些锚标签的“导航栏”。 我还在我的代码中包含了“text.php”文件。 此文件位于文件夹“texte”中。 锚标记应该将用户带到text.php中的位置,但它只加载404 - 找不到页面。 p>

我如何包含 file: p>

 &lt;?php include'texte / text.php';  ?&gt; 
  code>  pre> 
 
 

我声明我的“start-word”如下: p>

 &lt; a href =  “texte / text.php#InfoAnker”&gt; Informationen&lt; / a&gt; 
  code>  pre> 
 
 

我的锚点如下: p>

  &lt; h2 id =“InfoAnker”&gt;  Informationen&lt; / h2&gt; 
  code>  pre> 
 
 

我还尝试了&lt; a href =“#InfoAnker”&gt; Informationen&lt; / a&gt; code>,因为 包括但它得出了相同的结果。 p>

任何想法? p> div>

If <h2 id="InfoAnker"> Informationen </h2> is generated somewhere within your page and sent to the browser, then an anchor link should work correctly as

<a href="#InfoAnker">Informationen</a>

One think I noticed from your example is that you should make sure you use " not for quote marks - these are not the same characters, and you must use the correct one.

Clicking on an anchor link should not result in a 404 error, if it's correctly defined. Even if the anchor ID doesn't actually exist in the page, it still wouldn't give a 404 - the worst that would occur would be that nothing would happen.

Additionally to what @ADyson said:

Anchor links work on the browser side. The form #InfoAnker redirects to an anchor on the same page, while texte/text.php#InfoAnker redirects to another page. Again: on the browser side.

But I see, you are using includes, which work on server side. Normally, you don't have the same path and file structure on server and client (if your server include is located outside of wwwroot for example, but also for hiding confidential data and your program structure). This is most likely the reason why you see the 404. So if you want to access the piece of text that was included in your script, your redirect must not have a url.

But for this to work, you need to define the anchor itself, using <h2 id="InfoAnker"> Informationen</h2>, either in the include or in the file which includes text.php