我正在尝试使用php加载一些内容,而某些导航链接引用了锚标记。
当页面加载时,有一个导航列表,我可以点击将页面滚动到正确锚点的任何项目。如果我点击最后一项(论坛),它会加载不同的内容,我永远不会回到原始内容。
如果我将其中一个链接更改为href =“index.php?content = main,那么该链接将重新加载主要内容。因此,由于某种原因,锚标记#导致此失败,如果我更改了最后一个链接是href =“index.php?content = forum #test”,那么论坛内容也不会加载。
我正在犯一些根本性的错误,或者这不能以这种方式完成吗?
我可以右键单击链接并在新选项卡中打开它们可以正常工作并滚动到正确的锚点。左键单击似乎什么也没做,甚至没有更改地址栏。
这是代码。
<!-- navigation menu -->
<ul id="navigation">
<li><a href="index.php?content=main#action" class="active">Living The Dream</a></li>
<li><a href="index.php?content=main#about">About</a></li>
<li><a href="index.php?content=main#reality">The Reality</a></li>
<li><a href="index.php?content=main#setup">Downloads & Setup</a></li>
<li><a href="index.php?content=forum">Forum</a></li>
</ul>
<!-- end of navigation menu -->
</div>
<!-- end of the sidebar -->
<!-- content container -->
<div id="container">
<?php
if (isset($_GET['content']))
{
$parts = explode('#', $_GET['content']);
print_r($parts);
$content = $parts[0];
$content .= '.php';
}
else
{
$content="main.php";
}
$allowed = Array();
$dh = opendir(".");
while (false !== ($file = readdir($dh)))
{
if (!is_dir("./$file"))
{
$expl = explode('.',$file);
if ($expl[1] == "php")
{
$allowed[] = $file;
}
}
}
closedir($dh);
print($content);
if (in_array($content, $allowed))
{
include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.$content);
}
else
{
include($_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR."main.php");
}
?>
答案 0 :(得分:1)
您想在查询字符串中使用#
吗?只需对其进行编码index.php?content=main%23action
片段未在HTTP请求中发送。原因是片段标识符仅供浏览器使用。
答案 1 :(得分:0)
DarkBee有了答案,任何带有#的链接都不会传递给服务器,而是由浏览器处理。看起来我需要根据内容重新编写链接。
这就是我目前正在做的事情。我对PHP知之甚少,所以任何人都有更好的方式让我知道。
在我的index.php的最后,我有这个代码:
<?php
# Used as part of the navigation solution
# see nav.php
echo "<script type='text/javascript'>";
echo "element_to_scroll_to = document.getElementById('".$_GET['a']."');";
echo "element_to_scroll_to.scrollIntoView();";
echo "</script>";
?>
&#13;
在我的nav.php中我有这个。
<ul id="navigation">
<?php
# Take an array of contents and links.
# Depending on current content page write the links
$nc = explode('.',$content);
$navs[0] = array("main", "action", "Living The Dream");
$navs[1] = array("main", "about", "About");
$navs[2] = array("main", "reality", "The Reality");
$navs[3] = array("main", "setup", "Downloads & Setup");
$navs[4] = array("forum", "", "Forum");
foreach ($navs as $link) {
if ($link[0] == $nc[0]) {
echo('<li><a href="#'.$link[1].'">'.$link[2].'</a></li>');
} else {
echo('<li><a href="index.php?content='.$link[0].'&a='.$link[1].'">'.$link[2].'</a></li>');
}
}
?>
</ul>
&#13;