簡體   English   中英

基於PHP變量的HTML更改CSS樣式

[英]HTML change css Style Based on PHP Variable

我有一個包含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以上的變量。 現在,我將其設置為“ blue”,因為我在CSS中將id設置為blue,因此它們應該匹配,並且單詞應以藍色回顯,但文本不會改變顏色。

  1. 您已經在PHP中,因此應刪除<?php?>
  2. 單引號中的變量是文字文本,而不是變量。 PHP用雙引號將其擴展為該值。
  3. 您的變量是$textcolor ,而不是$blue

嘗試:

echo ($x . " degrees F is equal to " . round($result, 1) . " C, which is " . '<span id="' . $textcolor . '" >' . $tempdesc . '</span>' ."<br/>\n");

也許您想要的是更改內聯樣式。

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");

} 

確保在變量周圍使用花括號。

您沒有任何變量($ blue)

請只替換這行

<span style="color:'.$textcolor.'" >

使用sass怎么樣? 您可以將變量用作顏色或其中的任何內容!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM