简体   繁体   English

在菜单中的当前页面上设置CSS类

[英]Set a CSS class on the current page in a menu

I have a static website, but I have included different bits of the page (head, header, image slider, footer) by php's include function, to make the website manageable. 我有一个静态网站,但是我通过php的include函数包含了页面的不同部分(标题,标题,图像滑块,页脚),以使网站易于管理。 Now the problem is that the current page's name in the menu-bar must have selected="selected" to show the current page's name pressed in the front end. 现在的问题是,菜单栏中的当前页面名称必须已选择=“ selected”才能显示在前端按下的当前页面名称。

I made a solution of giving every menu-item an id and than injecting the following code to end of every page and setting that page's id (the home button id selected in the example code). 我提出了一个解决方案,为每个菜单项提供一个ID,然后将以下代码注入到每个页面的末尾并设置该页面的ID(在示例代码中选择的主页按钮ID)。

<script type="text/javascript">
    $(document).ready(function() {
        $('#home').addClass('active');
    });
</script>

Now the problem is that I must include this script in every page and to change the id to that page (eg in the above script I have pasted the code in the home page, 现在的问题是,我必须在每个页面中都包含此脚本,然后将ID更改为该页面(例如,在上面的脚本中,我已将代码粘贴到了首页中,

I wanna have a solution where I add only once chunk of the code to my footer.php and it automatically detect the current page and set the select="selected" for that menu-item. 我想提供一个解决方案,在该解决方案中,我只将一次代码块添加到footer.php中,它会自动检测当前页面并为该菜单项设置select =“ selected”。

Thank You all in advance... 谢谢大家...

In PHP, you can put the active class on your menu definition (if it's the same on every page), if you know the current page visited: 在PHP中,如果知道当前访问的页面,则可以将active类放在菜单定义中(如果每个页面上都相同):

<ul>
   <li <?php if($current_page == 'home') echo 'class="active"'; ?>>Home</li>
   <li <?php if($current_page == 'blog') echo 'class="active"'; ?>>Blog</li>
   <li <?php if($current_page == 'contact') echo 'class="active"'; ?>>Contact</li>
</ul>

and so on... You don't need Javascript for this (which is disable by some users). 依此类推...您不需要为此使用Javascript(某些用户已将其禁用)。 The only need is to get the $current_page variable, which can be based on the URL or the ID of the page, depending on your current website architecture. 唯一需要的是获取$current_page变量,该变量可以基于URL或页面的ID,具体取决于您当前的网站体系结构。

The solution I discovered for my (specific) problem is, 我针对(特定)问题发现的解决方案是,
I gave id(s) to all the menu-items, and those ids were the namesake of the related files (the files to which they'll link), for example the button which links to the home.php was given the id of "home" and the button which was linking to the profile.php was given the id of "profile" than included the following code in the footer 我给所有菜单项都指定了id,这些id是相关文件(它们将链接到的文件)的名称,例如,链接到home.php的按钮的ID为“ home”和链接到profile.php的按钮的ID为“ profile”,而不是在页脚中包含以下代码

<?php
$php_self = $_SERVER['PHP_SELF'];
$basename = basename($php_self);
$id = basename($basename, ".php");
?>

<script type="text/javascript">
   $(document).ready(function() {
      $('#<?php echo $id; ?>.addClass('active');
   });
</script>

( jQuery used, but if the website doesn't have jQuery already, than the document.getElementById must be used ). (使用jQuery,但如果网站尚没有jQuery,则必须使用document.getElementById)。

Thank You all for Your time.. 谢谢大家的时间..

You can use js and php together: 您可以同时使用js和php:

$(document).ready(function(){
    $("#<?=$current_page?>").addClass('active')
})

It's shorter than Maxime Lorant's code... 它比Maxime Lorant的代码短...

Or if your url is something like site.com/#home , this is the solution: Get css id from url (without php/js) 或者,如果您的网址类似于site.com/#home ,则这是解决方案: 从网址获取CSS ID (无需php / js)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM