[英]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,因此它們應該匹配,並且單詞應以藍色回顯,但文本不會改變顏色。
<?php
和?>
。 $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.