基于PHP变量定义CSS属性值

时间:2015-09-03 06:06:16

标签: php html css

我想基于PHP变量为我的tr元素定义font-weight。我在这段代码中做错了什么?

<?
    $vartest = 1;
?>

<table>
  <tr style="font-weight: <? ($vartest === 1) ? echo bold : echo normal ?>">
    <td>aaaaaaa</td>
    <td>bbbbbbb</td>                                        
  </tr>
</table>

5 个答案:

答案 0 :(得分:4)

试试这个:

<?php

    $vartest = 1;
?>

<table>
  <tr style="font-weight: <?php echo ($vartest === 1) ? 'bold' : 'normal' ?>">
    <td>aaaaaaa</td>
    <td>bbbbbbb</td>                                        
  </tr>
</table>

答案 1 :(得分:3)

三元表达是错误的。试试 -

<tr style="font-weight: <? echo ($vartest === 1) ? 'bold' : 'normal'; ?>">

答案 2 :(得分:0)

尝试以下

qryLibrary["ID"]

让我知道它是否有用

答案 3 :(得分:0)

在语法中为String添加""

<tr style="font-weight: <? echo ($vartest === 1) ?  "bold" :  "normal" ?>">

答案 4 :(得分:0)

也许你可以这样做,使用css进行预定义:

<?php
$Class = "bold";
?>
<html>
<head>
    <style>
        tr.bold{
            font-weight: bold;
        }

        tr.normal{
            font-weight: normal;
        }
    </style>
</head>

<body>
    <table>
        <tr class="<?php echo $Class; ?>">
            <td>abc</td>
            <td>def</td>
        </tr>
    </table>
</body>