如何根据PHP代码或URL方向的变体激活CSS属性? [英] How to activate a CSS property according to a variation in PHP code or URL direction?
问题描述
我使用PHP数组和 lang?=
更改网站中的语言。当用户点击链接以更改网站的语言时,我想让此链接保持按下或更改为不同的颜色,因此用户知道他/她在哪个版本的网站。如何在这种情况下激活CSS属性?
I change the language in my site using PHP arrays and lang?=
. When the user clicks a link to change the language of the site I want this link to keep "pressed" or change to a different color, so the user knows in which version of the site he/she is. How can I activate a CSS property in this situation?
common.php:
<?php
session_start();
header('Cache-control: private'); // IE 6 FIX
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
// register the session and set the cookie
$_SESSION['lang'] = $lang;
setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}
switch ($lang) {
case 'en':
$lang_file = 'lang.en.php';
break;
case 'es':
$lang_file = 'lang.es.php';
break;
case 'tw':
$lang_file = 'lang.tw.php';
break;
case 'cn':
$lang_file = 'lang.cn.php';
break;
default:
$lang_file = 'lang.en.php';
}
include_once 'languages/'.$lang_file;
?>
lang.en.php:
<?php
$lang = array(
'h1' => 'Hello World',
);
?>
index.php:
<ul id="lang">
<li><a href="index.php?lang=en">English</a></li>
<li><a href="index.php?lang=es">Español</a></li>
<li><a href="index.php?lang=tw">中文(繁體)</a></li>
<li><a href="index.php?lang=cn">中文(简体)</a></li>
</ul>
推荐答案
您可以测试 $ lang
,并在对应于该语言的链接上添加CSS类:
You could test the value of $lang
in the portion of code that generates the HTML output, and add a CSS-class on the link which corresponds to that language :
<ul id="lang">
<li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current_language"';} ?>>English</a></li>
<li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current_language"';} ?>>Español</a></li>
<li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current_language"';} ?>>中文(繁體)</a></li>
<li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current_language"';} ?>>中文(简体)</a></li>
</ul>
根据 $ lang
的值,四个链接之一将具有CSS类 current_language
。您可以在CSS文件中设置它,以便突出显示具有它的链接。
Depending on the value of $lang
, one of the four links would have the CSS class current_language
. Up to you to set it in your CSS file so it highlights the link which has it.
生成的HTML将如下所示: em>( $ lang
是'en'
):
<ul id="lang">
<li><a href="index.php?lang=en" class="current_language">English</a></li>
<li><a href="index.php?lang=es" >Español</a></li>
<li><a href="index.php?lang=tw" >中文(繁體)</a></li>
<li><a href="index.php?lang=cn" >中文(简体)</a></li>
</ul>
(当然, $ lang
变量从生成HTML输出的代码部分中可见)
(Of course, you'll have to make sure the $lang
variable is visible from the portion of code that generates the HTML output)
这篇关于如何根据PHP代码或URL方向的变体激活CSS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!