代码之家  ›  专栏  ›  技术社区  ›  Martheli

HTML基于PHP变量更改CSS样式

  •  1
  • Martheli  · 技术社区  · 6 年前

    我有一个包含HTML和PHP的PHP页面。我有以下代码:

    for ($x = 0; $x <= 100; $x++) {
    
        $result = convertTemp($x);
    
        $tempdesc = getTempName($x);
    
        $textcolor = "blue";
    
        echo ($x . " degrees F is equal to " . round($result, 1) . " C, which is " . '<span id="<?php $textcolor ?>" >' . $tempdesc . '</span>' ."<br/>\n");
    
    } 
    

    我想要个变量 tempdesc 不同颜色的回声 $textcolor 上面的变量。现在我把它设置为“蓝色”,因为我的CSS中有一个ID设置为蓝色,所以它们应该匹配,单词应该用蓝色来回响,但是文本不会改变颜色。

    4 回复  |  直到 6 年前
        1
  •  2
  •   user3783243    6 年前
    1. <?php ?>
    2. $textcolor $blue

    echo ($x . " degrees F is equal to " . round($result, 1) . " C, which is " . '<span id="' . $textcolor . '" >' . $tempdesc . '</span>' ."<br/>\n");
    
        2
  •  0
  •   Nabeel Akbar    6 年前

    <span style="color:'.$textcolor.'" >
    
        3
  •  0
  •   João Santos    6 年前

    for ($x = 0; $x <= 100; $x++) {
    
        $result = convertTemp($x);
    
        $tempdesc = getTempName($x);
    
        $textcolor = "blue";
    
        echo ("{$x} degrees F is equal to {round($result, 1)} C, which is " . 
    '<span style="color:{$textcolor}">' . "{$tempdesc}" . '</span>' ."<br/>\n");
    
    } 
    

        4
  •  0
  •   Subrata    6 年前