这里需要您的帮助。正如标题所示,我正在尝试使用 PHP 变量更改 css 类。所以基本上我想创建一个回显某些代码的循环。但我希望第一个循环中的 div 类有所不同——它应该被隐藏。
这是我为使问题变得清晰而编写的简化代码。我不知道错误在哪里。请指教。
<!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>
您需要将 if 条件放入循环中
<?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>";
}
?>
因为有一个条件当 $page == 0 那么它会给出一个隐藏类,所以如果你只是把它放在循环之外,那么 $page 永远不会得到 int 值,因为在条件的顶部有 $page =“”;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)