【问题标题】:php script to change button class if page opened如果页面打开,php脚本更改按钮类
【发布时间】:2013-01-07 12:57:44
【问题描述】:

我正在尝试让这个脚本工作,但不幸的是无事可做...... 我也认为有更好的方法可以做到这一点,但不知道哪个?

所以我希望在打开的页面上按钮保持悬停状态,所以我创建了一个看起来像按钮悬停的类,并说如果当前页面名称对应于“此按钮”,则 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 在您的代码中未定义。
  • 你没有任何 CSS 类“buttonselected”。

标签: php html css class button


【解决方案1】:

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

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

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

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

【讨论】:

  • 对不起,这是标题。我在放置 include header.php 之前在文件上定义了 $currentpath。我设置了 $currentpath = basename(FILE); (使用 __(它不发布)返回文件名 basename($prezzi 和其他)也返回文件名。
猜你喜欢
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-20
  • 2011-10-08
  • 2012-01-28
  • 2016-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多