使用php变量更改css类 [英] Change css class using php variable
问题描述
下面是我为解决问题所做的简化代码。我不知道错误在哪里。请指教。
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp:// www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>无标题文档< / title>
< style>
.hidden {
visibility:hidden;
}
.visible {
visibility:visible;
}
.firstclass {
color:red;
}
< / style>
< / head>
< body>
<?php
$ case = 4;
$ page =;
$ class =;
if($ page == 0){
$ class =hidden;
}
else {
$ class =visible; ($ page = 0; $ page <$ case; $ page ++){
echo< div class =& quot; firstclass $ class& amp; quot;
} ;">这是页面$ page< br />< / div>;
}
?>
< / body>
< / html>
您需要将if条件放入循环$>
<?php
for($ page = 0; $ page< $ case; $ page ++){
if($ page == 0)$ class =hidden;
else $ class =visible;
echo< div class ='firstclass $ class'>这是页面$ page< br />< / div>;
}
?>
因为当$ page == 0有条件的时候它会给一个隐藏的类,所以如果你只是把它放在循环之外,那么$ page永远不会获得一个int值,因为在条件的顶部有$ page =;
need your kind help here. So as the title suggest, I'm trying to change css classes using PHP variable. So basically I want to create a loop that echos some code. But I want the div class in the first loop to be different -- it should be hidden.
Here's the simplified code I made to make the problem clear. I don't know where the mistakes are. Please advise.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
.hidden {
visibility:hidden;
}
.visible {
visibility:visible;
}
.firstclass {
color:red;
}
</style>
</head>
<body>
<?php
$case = 4;
$page = "";
$class = "";
if ($page == 0) {
$class = "hidden";
}
else {
$class = "visible";
}
for ($page = 0; $page < $case; $page++) {
echo "<div class = " firstclass $class " >This is page $page <br /></div>";
}
?>
</body>
</html>
You need to put the if condition in the looping
<?php
for ( $page = 0; $page < $case; $page++ ) {
if ($page == 0) $class = "hidden";
else $class = "visible";
echo "<div class='firstclass $class'>This is page $page <br /></div>";
}
?>
Because there is a condition when $page == 0 then it will give a hidden class, so if you just put it outside the looping, then $page never get the an int value, because on the top of the condition there is $page = "";
这篇关于使用php变量更改css类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!