如果页面打开更改按钮类的 php 脚本

标签 php html css class button

我正在尝试让这个脚本工作,但遗憾的是无能为力... 我也认为有更好的方法来做到这一点,但不知道是哪个?

所以我希望在打开的页面上按钮保持悬停状态,所以我创建了一个看起来像悬停按钮的类,并说如果当前页面名称对应于“此按钮”,则 php 将应用此类链接。

$currentpath = basename(__FILE__);

在我包含 header.php 之前在每个页面上设置

谢谢!

PHP 和 HTML:

<?h
$o = "/ita/o.h";
$ = "/ita/.h";
$a = "/ita/a.h";
$s = "/ita/s.h";
$st = "/ita/st.h";
$p = "/ita/p";
$t = "/ita/t";
$c = "/ita/c";
?>

<div id="button"> 
  <ul> 
    <li><a href=<?=$o?> class="<?php if($currentpath == basename($o)){echo("buttonon");} ?>">o</a></li>
    <li><a href=<?=$p?> class="<?php if($currentpath == basename($p)){echo("buttonon");} ?>">p</a></li>
    <li><a href=<?=$a?> class="<?php if($currentpath == basename($a)){echo("buttonon");} ?>">a</a></li>
    <li><a href=<?=$s?> class="<?php if($currentpath == basename($s)){echo("buttonon");} ?>">s</a></li>
    <li><a href=<?=$st?> class="<?php if($currentpath == basename($st)){echo("buttonon");} ?>">st</a></li>
    <li><a href=<?=$p?> class="<?php if($currentpath == basename($p)){echo("buttonon");} ?>">p</a></li>
    <li><a href=<?=$t?> class="<?php if($currentpath == basename($t)){echo("buttonon");} ?>">t</a></li>
    <li><a href=<?=$c?> class="<?php if($currentpath == basename($c)){echo("buttonon");} ?>">c</a></li>
  </ul> 
</div>  

CSS:

#button ul { 
        margin: 0;
        padding: 0; 
        list-style-type: none; 
        text-align: center; 
        background-color: #F00;
} 
#button ul li {  
        display: inline; 
} 
#button ul li a { 
        display:inline-block;
        color:#FFF;
        font-family:Calibri;
        font-size:18px;
        font-weight:bold;
        padding:4px 15px 4px 15px;
        text-decoration:none;
        font-variant:small-caps;
} 
#button ul li a:hover { 
        background-color:#F00;
        color:#02346F;
        padding:4px 15px 4px 15px;
        font-size:18px;
        font-weight:bolder;
}

.buttonon { 
        background-color:#F00;
        display:inline-block;
        font-family:Calibri;
        font-size:18px;
        font-weight:bold;
        text-decoration:none;
        font-variant:small-caps;
        color:#02346F;
        padding:4px 15px 4px 15px;
        font-size:18px;
        font-weight:bolder;
}

编辑:我为 $currentpath 添加了一个定义。

最佳答案

假设 $currentpath 在您的代码的其他地方定义:

使用 basename() 将不起作用,因为您有子目录,而 basename() 不会返回子目录。

你可以搜索一个子字符串:

<?php if(strpos($currentpath, $prezzi) !== false){echo("buttonselected");} ?>

关于如果页面打开更改按钮类的 php 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14490011/

相关文章:

jQuery 是与 Web 表单还是 HTML 表单一起使用?

jquery - 定位不同大小/比例的img并填充到div中

php - 如何将自定义字段添加到 WooCommerce 注册表单

php - 如何将json发送到php文件以保存在服务器上

php - 从 mysql 查询中打印随机行

php - 将 google reCaptcha 集成到现有的付款表单中

javascript - 将元素向上移动 2 个位置

jquery - 缓存为空时获取音频文件的持续时间属性的问题

css - 哪些 CSS 属性是打印出来的,哪些不是?

html - 固定菜单链接在 <article> 标签内不起作用