Javascript PHP 使用包含在页面上设置活动链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8476225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:08:49  来源:igfitidea点击:

PHP set active link on page using includes

phpjavascriptjqueryincludenavigation

提问by nowayyy

So I have searched SO for an hour trying to find the answer, and also tried various methods, including this

所以我搜索了一个小时试图找到答案,并尝试了各种方法,包括这个

I am trying to include my pages, along with the navigation. But on the correct page, I need the list-item to have a class of active. The navigation is in header.php and currently looks like this:

我正在尝试包含我的页面以及导航。但是在正确的页面上,我需要列表项具有active. 导航在 header.php 中,目前看起来像这样:

<nav>
    <ul>
      <li class="active"> <a href="/">Home</a> </li>
      <li> <a href="#">Apps</a> </li>
      <li> <a href="#">Forums</a> </li>
    </ul>
  </nav>

First, I have no idea if JS(jQuery) would be better, or if PHP was to be better. Both wouldn't matter if it works.

首先,我不知道 JS(jQuery) 是否会更好,或者 PHP 是否会更好。如果它有效,两者都无关紧要。

Also I am a bit new with PHP and trying to learn.

此外,我对 PHP 有点陌生,正在努力学习。

What would be the easiest (hopefully) method to use? So I don't have to change a lot of code just to give a nav class="active"

什么是最简单(希望)使用的方法?所以我不必为了提供导航而更改很多代码class="active"

回答by Naftali aka Neal

Asumming you have a $pagevariable (which contains the name of the page you are currently on):

假设您有一个$page变量(其中包含您当前所在页面的名称):

  <nav>
    <ul>
      <li class="<?php echo ($page == "home" ? "active" : "")?>"> <a href="/">Home</a> </li>
      <li class="<?php echo ($page == "apps" ? "active" : "")?>"> <a href="#">Apps</a> </li>
      <li class="<?php echo ($page == "forums" ? "active" : "")?>"> <a href="#">Forums</a> </li>
    </ul>
  </nav>

回答by Niklas Andersson

Here is a simple way where you do not need to add any other variable

这是一种无需添加任何其他变量的简单方法

<li class="<?php echo ($_SERVER['PHP_SELF'] == "/index.php" ? "active" : "");?>">
<a href="index.php">Start</a>
</li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/about.php" ? "active" : "");?>">
<a href="about.php">About</a>
</li>
<li class="<?php echo ($_SERVER['PHP_SELF'] == "/practices.php" ? "active" : "");?>">
<a href="practices.php">Practices</a>
</li>

回答by Niko

If you don't want to use jQuery or PHP - can do next:

如果您不想使用 jQuery 或 PHP - 可以执行以下操作:

  1. Give ID or different CLASS to each <li>element in "your-include-nav.php".
  2. Define the "active" style with CSS in <head>section, on each page.
  1. <li>“your-include-nav.php”中的每个元素提供ID 或不同的 CLASS 。
  2. <head>在每个页面上的部分中使用 CSS 定义“活动”样式。

回答by Harshvardhan Singh Baghel

define variable $page="index.php" in index.php page and for other pages change the variable value according to the page name

在 index.php 页面中定义变量 $page="index.php" 其他页面根据页面名称更改变量值

<li class="<?php echo ($page == "index.php" ? "active" : "")?>">
    <a href="index.php">Home</a>
</li>
<li class="<?php echo ($page == "about.php" ? "active" : "")?>">
    <a href="about.php">About</a>
</li>
<li class="<?php echo ($page == "service.php" ? "active" : "")?>">
    <a href="service.php">Services</a>
</li>
<li class="<?php echo ($page == "contact.php" ? "active" : "")?>">
    <a href="contact.php">Contact</a>
</li>

回答by Hamza Khan

Add basename function. Then it will work i hope.

添加基名功能。然后它会起作用,我希望。

回答by Hamza Khan

Add basenamefunction before $_SERVER. I hope it will work.

basename之前添加功能$_SERVER。我希望它会起作用。

echo (basename($_SERVER['PHP_SELF']) == 'yourPageName' ?'active' : " ");

回答by Alexander Zemskov

$page_url = $_SERVER['QUERY_STRING'];

$s = explode("&",$page_url);
//print $s[0];
$page = $s[0];

function createTopNav($page)
{
    $pages = array(
        array(
            'name'=>'Feeder',
            'link'=>'page=feeder'
        ),
        array(
            'name'=>'Services',
            'link'=>'page=services'
        ),
        array(
            'name'=>'Development',
            'link'=>'page=development'
        ),
        array(
            'name'=>'Design',
            'link'=>'page=design'
        ),

    );
    $res = "";
    foreach($pages as $key=>$val)
    {
        if($val['link']==$page)
        {
            $res.= "<a class=\"active\" href=\"?"
            .$val['link'].
            "\">"
            .$val['name'].
            "</a>";
        }
        else
        {
            $res.= "<a class=\"\" href=\"?"
            .$val['link'].
            "\" >"
            .$val['name'].
            "</a>";
    }
    }
    $res.="";
    return $res;
}

echo createTopNav($page);

回答by Punit

if you are using query string exmaple.com?page_id=Appsto pass page id than with php you can approach this thing

如果您使用查询字符串exmaple.com?page_id=Apps来传递页面 id 而不是使用 php,您可以处理这个问题

$page_id = $_REQUEST['page_id'];

$page_id = $_REQUEST['page_id'];

 <ul>
          <li class="<?php echo ($page_id == "Home" ? "active" : "");?>">
 <a href="/">Home</a>
          </li>
          <li class="<?php echo ($page_id == "Apps" ? "active" : "");?>">
 <a href="#">Apps</a>
          </li>
          <li class="<?php echo ($page_id == "Forums" ? "active" : ""); ?>">
 <a href="#">Forums</a>
          </li>
        </ul>

回答by lightofsouls

At page header use:

在页眉使用:

<?php $loc = this.location; ?>

Then at every link add:

然后在每个链接添加:

<?php(this.href == $loc) ? echo 'class="active"' : '' ?>